Skip to content Skip to sidebar Skip to footer

Change Or Swap The Css Of Multiple Html Elements At Once

I have about 100 , 100 and 100 tags in my document. I need to implement the following operations in JavaSc

Solution 1:

The simplest way is to use CSS to do this, rather than changing the element classnames. Consider the following markup and CSS.

.normal .foo{
    background-color: #0f0;
}
.alternate .foo {
    background-color: #f00;
}

<bodyclass="normal"><spanclass="foo">hello</span><spanclass="bar">hello</span><spanclass="baz">hello</span></body>

You can simply use javascript to change the classname on the body from normal to alternate, to implement the color change on .foo elements. More rules will set colors for bar and baz.

document.getElementsByTagName('body')[0].className = 'alternate';

Solution 2:

You could give the body of your document a class to determine which configuration of colours should be used and then simply style the spans accordingly.

eg:

.configTypeOnespan.foo{...}
.configTypeTwospan.foo{...}

If you're then changing the styles on the same page after some period of time, a small piece of JS to change the class of the body will be all that's required.

Post a Comment for "Change Or Swap The Css Of Multiple Html Elements At Once"