admin管理员组

文章数量:1296298

For example if i have

<style type='text/css' id='divcolorgreen'>
    div{
        color: green;
    }
</style>

I can disable the css rule using 3 ways

  • removing the style element (reappend it to reenable)
  • modifying it's inner html
  • add the inline rule one by one to each elements (better using jquery)

Is there any easier way to disable/remove the css rule?

For example if i have

<style type='text/css' id='divcolorgreen'>
    div{
        color: green;
    }
</style>

I can disable the css rule using 3 ways

  • removing the style element (reappend it to reenable)
  • modifying it's inner html
  • add the inline rule one by one to each elements (better using jquery)

Is there any easier way to disable/remove the css rule?

Share Improve this question edited Jul 30, 2013 at 20:07 Ryan B 3,39222 silver badges35 bronze badges asked Sep 13, 2012 at 2:52 Najib RazakNajib Razak 1491 gold badge2 silver badges8 bronze badges 2
  • i found the easiest way already. it's totally different to the accepted answer i tick below. i hope u all find it out soon – Najib Razak Commented Oct 19, 2012 at 8:10
  • What did you use finally? After all, you could add your own answer, or mention it in the ments. This site is aimed at providing help to other users, so sharing your knowledge is definitely desired :) Cheers! – Tadeck Commented Nov 2, 2014 at 22:21
Add a ment  | 

3 Answers 3

Reset to default 7

This is the purpose of classes. By assigning a class eg. to the <body> tag, you get the same functionality:

<style type='text/css' id='divcolorgreen'>
    body.divcolorgreen div{
        color: green;
    }
</style>

And then if <body> looks like this:

<body class="divcolorgreen">
    ...
</body>

the rule is applied. To disable the rule, remove the mentioned class:

<body>
    ...
</body>

OK, so you are trying to change the color of all divs on a page.

jQuery has .css() which lets you set the style of all elements that match the selector. In your case, it's just div.

To set:

$('div').css('color', 'yellow');

To remove:

$('div').css('color', '');

If you don't want to use jQuery, the idea is the same:

document.getElementsByTagName('div') gives you all divs on the page. You can loop through them and change their style by elem.style.color="yellow";

1 you can do this by jquery css( propertyName , value ) api but according to me the easier way because if there is many style like you have set the font ,color ,height,...the second method is easier and lot more time saving

 $("div").css("color", "yellow")

2 you can do this by removeClass('someClass'); and addClass('someClass');

for example, use a jQuery selector to target the division element with the class "some"

html

<div class="some" ></div>

css

.some{ color: green; }

jquery

if you want to add another class can use the addClass

$(".some").click( function(){
   $(this).addClass('someClass');
});

If you would like to remove the class, use jQuery's removeClass method:

$(".some").click( function(){
   $(this).removeClass('someClass');
});

本文标签: javascriptHow to disable and enable css ruleStack Overflow