Get the js function currentStyle (IE) and defaultView (FF) _ javascript in the css style sheet.

Source: Internet
Author: User
JS gets the value from the style sheet function in IE using currentStyle and firefox ultview in firefox. For more information, see. However, DOM. style can only be accessed. In this way, the built-in style in the label, if the style is written in In the. CSS file, you can read the style.

In fact, there are other ways to read the style information. There are two methods, one is through the document. styleSheets object, and the other is through the "final style" object. In IE, this object is called currentStyle, and in FF, this object is called document. defaultView. I packed these two classes and made a function for accessing style information, as shown below:

The Code is as follows:


// ============================ Access to the style sheet function ========== ==================================
Function returnStyle (obj, styleName ){
Var myObj = typeof obj = "string "? Document. getElementById (obj): obj;
If (document. all ){
Return eval ("myObj. currentStyle." + styleName );
} Else {
Return eval ("document. defaultView. getComputedStyle (myObj, null)." + styleName );
}
}


The function has two parameters:

Obj: The accessed object. Its type is DOM object or its id;

StyleName: name of the style to be accessed. The type is string, but the name cannot use the "-" number. The name must be written in case-insensitive format like the attribute name of style. Object. For example, background-color must be written as backgroundColor.

The Return Value of the function is of the string type.

Note: This method can only access style files and cannot be written. To write a style, use DOM. style. XXX. In addition, there are some style access problems in FF, such as padding and margin. If the padding and margin values are set in the style, we can use marginLeft to return values.

The Code is as follows:







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.