Code for accessing the style sheet using JavaScript

Source: Internet
Author: User

For example, when we move the mouse over an icon, the icon will have some animation effects (gradient amplification, flashing, changing colors, etc ), the friendly effects are basically related to styles. Therefore, I learned how to operate styles in JavaScript.

Key technical point: style. This article requires us to have a certain understanding of CSS styles. (You can google the CSS content)

Procedure:

1. The page Code (including JS Code) is as follows:

Copy codeThe Code is as follows: <! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> Style Example </title>
<Style type = "text/css">
Div. special
{
Background-color: Red;
Height: 50px;
Width: 50px;
}
</Style>

<Script type = "text/javascript">
Function getBackGroundColor (){
Var oDiv = document. getElementById ("div1 ");
Alert (oDiv. style. backgroundColor );
}
</Script>
</Head>
<Body>
<Div id = "div1" class = "special"> </div>
<Input type = "button" value = "Get BackGround Color" onclick = "getBackGroundColor ();"/>
</Body>
</Html>

The javascript code obtains the div object and outputs the background color of the div object. Everything looks so perfect, but the effect is always disappointing, as shown below:

In this case, JavaScript accessing CSS styles in this way cannot achieve the effect, because CSS data is not stored in the style attribute, but stored in the class. So what we need to do next is: how to access the CSS class?

2. Fortunately, we found the document. styleSheets set. Document. styleSheets contains references to all style sheets and all <style> elements on the html page.

Because of the differences in browsers, the methods for accessing separate rules in the style sheet (rule: specify a CSS class, for example, div. special in the preceding html code) are different. DOM specifies a cssRules set for each style table, but IE names this set rules. Therefore, before getting a style set, the Code requires a technical change:

Copy codeThe Code is as follows: oCSSRules = document.stylesheets1_02.16.css Rules | document. styleSheets [0]. rules;

Each rule contains the selectorText feature. div. special is the value of selectorText in the preceding html rule. The rule also contains the style attribute. In this case, you can use the style attribute to obtain the background color. Let's look at the Code:

Copy codeThe Code is as follows: function getBackGroundColor (){
Var oCSSRules = document.stylesheets%0%.css Rules | document. styleSheets [0]. rules;
Alert (oCSSRules [0]. style. backgroundColor );
}

Code effect:

3. At this time, you can change the background color by modifying the style, but it is best not to do this, because you have changed the CSS class, other elements that reference the CSS class also change the background color. Therefore, if you need to modify the style, directly modify the style attribute of a single element, such as: document. getElementById ("div1 "). style. backgroundColor = "Blue ";

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.