最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Update CSS rule property value - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Feb 1, 2012 at 15:14 zafzaf 23.3k12 gold badges69 silver badges96 bronze badges 3
  • possible duplicate of Changing a CSS rule-set from Javascript – Michael Berkowski Commented Feb 1, 2012 at 15:18
  • Also see stackoverflow./questions/8224527/… – Michael Berkowski Commented Feb 1, 2012 at 15:18
  • when using toggleClass('tile') you are adding tile to existing class – mgraph Commented Feb 1, 2012 at 15:20
Add a ment  | 

9 Answers 9

Reset to default 4

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

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? :)

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

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

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

javascriptkit. - Changing external style sheets using the 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:

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

and then remove the class when necessary with jquery

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

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.

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);
} 
发布评论

评论列表(0)

  1. 暂无评论