我有以下代码
<span style="background-image:url('/product_images/attribute_value_images/78.thumbnail.jpg')" class="thumbnail"> </span>
我需要更改单词缩略图以供预览,可以用 jquery 实现吗?
到目前为止我有这个:
$("li.swatch span.thumbnail").each(function(){
$(this).css("background-image").replace(/thumbnail/ig, "preview")
;
});
它似乎在工作(如果我 alert() 我得到正确的值)但在页面中没有改变.. 我可以在哪里查找任何想法?提前谢谢
最佳答案
使用 .css(propertyName)
仅获取 propertyName 的 css 规则值。
因此在您的代码中,您正在获取 css,替换其内容,但之后不会更新回 css 规则。
获取/修改css规则后使用另一个重载.css(propertyName, value)
更新规则:
$("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在 .css() 上。
关于jquery - 更改 css 属性的 1 个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8388413/