更新 CSS 规则属性值

发布于 2025-01-01 01:08:15 字数 532 浏览 0 评论 0原文

我有一个 html 元素,它的样式(使用 jquery)带有通过其类名定位的背景图像。

当我删除该类时,背景图像仍然存在 - 这不是我所期望或想要的。

test.html

<div id='log' class='tile'>HELLOWORLD</div>

test.css

.tile{
    background: none;
}

test.js

$('.tile').css("background-image", "url(tile.jpg)"); // We see image
$('#log').toggleClass('tile'); //  We still see image

在敲了敲我的头之后,我想我知道发生了什么。 css 应用于元素 - 而不是“类”。

如何定位特定的 CSS 规则以便更新其键值?

如果这是有道理的话。

I have a html element which is styled (using jquery) with a background image targeted thru its class name.

When I remove the class the background image stays - which is not what I expected or want.

test.html

<div id='log' class='tile'>HELLOWORLD</div>

test.css

.tile{
    background: none;
}

test.js

$('.tile').css("background-image", "url(tile.jpg)"); // We see image
$('#log').toggleClass('tile'); //  We still see image

After banging my head I think I know whats happening. The css is being applied to the element - NOT to the 'class'.

How can I target a specific css rule so that its key values can be updated?

If that makes sense.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

如梦 2025-01-08 01:08:15

如果你想改变“.tile”类的CSS规则,那么你可以这样做。
有一篇文章解释得非常清楚好吧:

function changeBackgroundImage(className, value){
        var ss = document.styleSheets;
        for (var i=0; i<ss.length; i++) {
            var ss = document.styleSheets;
            var rules = ss[i].cssRules || ss[i].rules;
            for (var j=0; j<rules.length; j++) {
                if (rules[j].selectorText === className) {
                    rules[j].style.backgroundImage = value;
                }
            }
        }
}

你可以这样称呼它:

changeBackgroundImage(".tile","url(tile.jpg)");

If you wan to change the css rules of the ".tile" class, then you can do it.
There is a post that explains it very well :

function changeBackgroundImage(className, value){
        var ss = document.styleSheets;
        for (var i=0; i<ss.length; i++) {
            var ss = document.styleSheets;
            var rules = ss[i].cssRules || ss[i].rules;
            for (var j=0; j<rules.length; j++) {
                if (rules[j].selectorText === className) {
                    rules[j].style.backgroundImage = value;
                }
            }
        }
}

You can call it like this :

changeBackgroundImage(".tile","url(tile.jpg)");
jJeQQOZ5 2025-01-08 01:08:15

问题是您将 background-image 设置为覆盖任何样式表规则的内联样式。切换类不会有任何影响。

您可以通过样式表规则设置背景,然后添加一个类来删除它;

#log {
  background-image: url(tile.jpg);
}
#log.tile {
  background: none;
}

或者您可以只使用 !important 作为;

.tile {
  background: none !important;
}

...可能是相反的情况,但你明白了吗? :)

The problem is that you´re setting the background-image as an inline stlye that overrides any stylesheet rules. Toggling the class won´t have any affect.

You can either have set the background through a styleheet rule and then add a class that removes it;

#log {
  background-image: url(tile.jpg);
}
#log.tile {
  background: none;
}

or you could just use !important as;

.tile {
  background: none !important;
}

...it might be the other way around but you get the point? :)

罗罗贝儿 2025-01-08 01:08:15

尝试删除类 tile 并使用 bg 应用新类:无

效果 - 需要时使用 bg 应用类,不需要时 - 不使用

try removing class tile and applying new class with bg: none

in effect - when needed apply class with bg, when not needed - without

无敌元气妹 2025-01-08 01:08:15

在这种情况下不需要 jQuery。您可以使用普通的旧 JavaScript。查看本教程:

javascriptkit.com - 使用 DOM 更改外部样式表

No need for jQuery in this case. You can use plain old JavaScript. Check out this tutorial:

javascriptkit.com - Changing external style sheets using the DOM

哑剧 2025-01-08 01:08:15

如果不重新编写样式表中的声明,则无法更改类本身,您只能使用选择器中的元素。

尝试:

$('.tile').css("background-image","none")

You can't change the class itself without re-writing that declaration in the stylesheet, you ARE working only with the element in the selector.

Try:

$('.tile').css("background-image","none")
空城旧梦 2025-01-08 01:08:15
$('#log').toggleClass('tile',true); 
$('#log').toggleClass('tile',true); 
八巷 2025-01-08 01:08:15

我会将背景图像作为 css 样式作为类的一部分:

 .tile {background-image: url('tile.jpg')};

然后在必要时使用 jquery 删除该类

 $('#log').removeClass('tile');

I would make the background image part of the class as a css style:

 .tile {background-image: url('tile.jpg')};

and then remove the class when necessary with jquery

 $('#log').removeClass('tile');
奶茶白久 2025-01-08 01:08:15

你可以在你的css中有两个类...

.tile{ 
    background: none; 
} 

.tile-w-image
{
    background-image: url(tile.jpg);
}

然后使用jquery只需切换这些类...

$("#log").toggleClass('tile').toggleClass('tile-w-image');

我确信这只是执行此操作的多种方法之一。我希望它有帮助。

you could have two classes in your css...

.tile{ 
    background: none; 
} 

.tile-w-image
{
    background-image: url(tile.jpg);
}

and then with jquery just toggle the classes...

$("#log").toggleClass('tile').toggleClass('tile-w-image');

I'm sure this is just one of many ways of doing this. I hope it helps.

柠檬心 2025-01-08 01:08:15

你们非常接近。
看起来您正在向元素添加内联 CSS,然后尝试切换该类。在大多数情况下,您应该将CSS样式分开:

HTML:

<div id='log' class='tile'>HELLOWORLD</div>

jQuery(我想这应该在单击或其他事件时完成):

$('#log').toggleClass('tile'); //  We still see image

如果“tile”类已经写入HTML,则切换它将删除它。

CSS:

.tile{ 
    background-image: url(tile.jpg);
} 

You are very close.
It seems like you are adding inline CSS to your element and then trying to toggle the class. You should keep CSS styling separate in most cases:

HTML:

<div id='log' class='tile'>HELLOWORLD</div>

jQuery (I imagine this should be done on click or another event):

$('#log').toggleClass('tile'); //  We still see image

If the "tile" class is already written to the HTML, then toggle-ing it will remove it.

CSS:

.tile{ 
    background-image: url(tile.jpg);
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文