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: