2011-12-05 12 views
3

私は次のコード変更1つの単語

<span style="background-image:url('/product_images/attribute_value_images/78.thumbnail.jpg')" class="thumbnail">&nbsp;</span> 

を持っている私は、プレビューのための単語のサムネイルを変更する必要があり、それはjQueryを使って可能ですか?

これまでのところ、私はこれがあります。

$("li.swatch span.thumbnail").each(function(){ 
    $(this).css("background-image").replace(/thumbnail/ig, "preview") 
    ; 
    }); 

(私が警告した場合、私は右の値を取得)()動作しているようだが、ページに変更されていません..私はアップになります任意のアイデアを?事前

答えて

10

でTHX .css(propertyName)を使用するだけpropertyNameの CSSルールの値を取得します。

あなたのコードでは、あなたのコンテンツは置き換えられますが、後でCSSルールを更新することはありません。

ルールを更新するためにCSSルールを変更/取得した後、他の過負荷.css(propertyName, value)を使用:の.cssに

$("li.swatch span.thumbnail").each(function() { 

    var $this = $(this), 
     // get the css rule and changing it 
     css = $this.css("background-image").replace(/thumbnail/ig, "preview"); 

    // update the css rule 
    $this.css('background-image', css); 

}); 

Documentationを()。

+0

:ありがとうございます:D –

関連する問題