Parse how js gets css styles and jscss styles

Source: Internet
Author: User

Parse how js gets css styles and jscss styles

1. Get inline styles

<Div id = "myDiv" style = "width: 100px; height: 100px; background-color: red; border: 1px solid black; "> </div> <script> var myDiv = document. getElementById ("myDiv"); alert (myDiv. style. width); // 100px alert (myDiv. style ['height']); // 100px var style = myDiv. style; alert (style. backgroundColor); // red myDiv. style. backgroundColor = 'green'; // the background color of myDiv changes to green. </script>

In this case, only the style attribute can be used to obtain and set the style, because element. the style attribute returns a set of style attributes and corresponding values similar to the array. Therefore, when accessing a specific style, you can use either of the following methods: "ele. style. attribute name "and" ele. style ['Property name'] ". However, it should be noted that the attribute names of background-color, margin-left, and other parallel bars in css styles, when you use the style attribute to obtain the set style, the name should be changed to the camper type, such as ele. style. backgroundColor.

2. Because the first method is to use the style attribute, only inline styles can be obtained.However, the actual situation is that all documents now basically follow the separation idea, and styles are basically external links, so all three styles must be taken into account. In this case, we need to use other methods to obtain them, in this case, different browsers have different processing methods (mainly ie and non-ie) for getting styles ),Therefore, the browser can be divided into two methods:

(2.1)Non-IE browser, Use document. the getComputedStyle (ele, null/pseudo class) method of the defaultView object. This method accepts two parameters. The first is the element to be investigated, and the second is based on the situation, if you only want to test the element itself, it is null. If you want to test the pseudo class, it is the pseudo class of the response. This method obtains the final style combination of the element application. It is also an instance similar to an array.

(2.2)In IE browserThe getComputedStyle () method is not supported, but each label element has an attribute similar to the currentStyle attribute, and its usage is the same as that of the style. However, the obtained style ranges are different. The method obtained by currenStyle is similar to the getComputedStyle () method.

To achieve compatibility in processing, you can create a function based on the two different processing methods to achieve compatibility, so that the style can be obtained successfully in any browser. As follows:

<Style type = "text/css"> # myDiv {background-color: blue; width: 100px; height: 200px ;} </style> <div id = "myDiv" style = "background-color: red; border: 1px solid black;"> </div> <script> var myDiv = document. getElementById ("myDiv"); var finalStyle = myDiv. currentStyle? MyDiv. currentStyle: document. defaultView. getComputedStyle (myDiv, null);/* determine whether currentStyle is supported (whether it is ie) to obtain style */alert (finalStyle. backgroundColor); // "red" alert (finalStyle. width); // "100px" alert (finalStyle. height); // "200px" </script>

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.