更改css属性的1个单词

Chr*_*sai 3 css jquery

我有以下代码

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

我需要更改单词缩略图以进行预览,有可能使用jquery吗?

到目前为止我有这个:

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

它似乎工作(如果我警告()我得到正确的价值)但是没有在页面中改变..任何想法,我可以查找?thx提前

Did*_*hys 10

使用.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 );

});
Run Code Online (Sandbox Code Playgroud)

关于.css()的文档.