简体   繁体   English

更新CSS规则属性值

[英]Update CSS rule property value

I have a html element which is styled (using jquery) with a background image targeted thru its class name. 我有一个html元素,其样式(使用jquery)与背景图像通过其类名称。

When I remove the class the background image stays - which is not what I expected or want. 当我删除课程时,背景图像会停留 - 这不是我期望或想要的。

test.html 的test.html

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

test.css test.css

.tile{
    background: none;
}

test.js 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'. css正在应用于元素 - 而不是'class'。

How can I target a specific css rule so that its key values can be updated? 如何定位特定的css规则以便更新其键值?

If that makes sense. 如果这是有道理的。

If you wan to change the css rules of the ".tile" class, then you can do it. 如果你想改变“.tile”类的css规则,那么你就可以做到。 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)");

The problem is that you´re setting the background-image as an inline stlye that overrides any stylesheet rules. 问题是您将background-image设置为覆盖任何样式表规则的内联stlye。 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; 或者你可以使用!important as;

.tile {
  background: none !important;
}

...it might be the other way around but you get the point? ......可能是另一种方式,但你明白了吗? :) :)

try removing class tile and applying new class with bg: none 尝试删除类tile并使用bg:none应用新类

in effect - when needed apply class with bg, when not needed - without 实际上 - 当需要时,在不需要时使用bg应用类 - 没有

No need for jQuery in this case. 在这种情况下不需要jQuery。 You can use plain old JavaScript. 您可以使用普通的旧JavaScript。 Check out this tutorial: 看看这个教程:

javascriptkit.com - Changing external style sheets using the DOM javascriptkit.com - 使用DOM更改外部样式表

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")
$('#log').toggleClass('tile',true); 

I would make the background image part of the class as a css style: 我会将背景图像作为css样式的一部分:

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

and then remove the class when necessary with jquery 然后在必要时使用jquery删除该类

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

you could have two classes in your css... 你可以在你的CSS中有两个班级......

.tile{ 
    background: none; 
} 

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

and then with jquery just toggle the classes... 然后用jquery切换类......

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

I'm sure this is just one of many ways of doing this. 我确信这只是众多方法中的一种。 I hope it helps. 我希望它有所帮助。

You are very close. 你很近。 It seems like you are adding inline CSS to your element and then trying to toggle the class. 您似乎在为元素添加内联CSS,然后尝试切换类。 You should keep CSS styling separate in most cases: 在大多数情况下,您应该将CSS样式分开:

HTML: HTML:

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

jQuery (I imagine this should be done on click or another event): jQuery(我想这应该在点击或其他事件上完成):

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

If the "tile" class is already written to the HTML, then toggle-ing it will remove it. 如果“tile”类已经写入HTML,则切换它将删除它。

CSS: CSS:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM