jquery - 更改 css 属性的 1 个单词

标签 jquery css

我有以下代码

    <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")
    ;
    });

它似乎在工作(如果我 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/

相关文章:

CSS 不透明度不适用于在 iframe 中打开的 Adob​​e pdf 阅读器

jQuery mobile,单页多页方法,多h1效果SEO,辅助功能和语义

jquery - 拖放 - 如何简化可拖放部分?

javascript - Chrome 上的 HTML5 canvas 和 php 无法正常工作

jquery - 如何动态引入背景颜色

javascript - 每次点击/提交按钮接受时,Cookie 消息仅显示一次

jquery - Bootstrap 3 : Show the error message with fixed Grid Layout

javascript - 用鼠标旋转div

css - 互联网浏览器

php - 动态缩小 CSS : leveraging browser caching