JS operation CSS style

Source: Internet
Author: User

Before introducing JS operations on CSS styles, I would like to introduce how CSS styles are loaded in HTML of webpages. Loading CSS styles on HTML pages can be divided into three forms: inline style, internal style sheet, and external style sheet ).

Embedded styles are used to directly add the style attribute to the DOM element to change the table element effect, for example, <Div style = "color: # f00"> embedded styles </div>. It can only change the style of this element.

An internal style adds a CSS style to the page. For example, add <style type = "CSS/text"> Div {color: # f00} </style> to the page. It can change the effect of the entire page element.

External style is used to load an external CSS style file. There are two ways to load external styles: <LINK rel = "stylesheet" type = "text/CSS" href = "test.css"/>, the other is: <style type = "CSS/text"> @ import test.css </style>.

Finally, let's talk about how JS operates CSS styles.

1. js operations embedded style.

<SCRIPT type = "text/JavaScript">

Document. getelementbyid ('test'). style. Color = '#000 ';

</SCRIPT>

2. js operation internal style.

<SCRIPT type = "text/JavaScript">

Method 1:

 

Function getstyle (selector, ATTR ){ For (I = 0, Len = Document. stylesheets. length; I <Len; I ++ ){ VaR rules; If (document. stylesheets [I]. Rules ){ /* For IE */ Rules = Document. stylesheets [I]. Rules; } Else { /* For non-ie */ Rules = document.stylesheetspolici2.16.css rules; } For (j = 0, rlen = rules. length; j <rlen; j ++ ){ If (rules [J]. selectortext = selector ){ Alert (rules [J]. Style [ATTR]); } } }}

 

</SCRIPT>

Method 2:

<SCRIPT type = "text/JavaScript">

IE: elementobj. currentstyle

W3C standard: window. getcomputedstyle (Elementobj, null) Or document. defaultview. getcomputedstyle (elementobj, null)

Document. defaultview. getcomputedstyle?Document. defaultview. Getcomputedstyle (ID, null). getpropertyvalue ("background-color"): Id. currentstyle ["backgroundcolor"];

</Scirpt>
 

3. js operation external style.

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.