Obtain the js functions currentStyle (IE) and defaultView (FF) of the style in the css style sheet)

Source: Internet
Author: User

However, DOM. style can only access <DOM style = ""> </DOM>, so that the built-in style in the label is, if the style is written in the <style type = "text/css"> </style> and .css files, 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:Copy codeThe 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.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>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Style type = "text/css">
# Demo {background-color: #000; padding: 10px; color: # fff; width: 200px ;}
</Style>
<Script type = "text/javascript">
// ================================ Access to the style sheet ========== ======================================
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 );
}
}
</Script>
<Title> </title>
</Head>
<Body>
<Div id = "demo"> here is the test content </div>
<Br/>
<A href = "###" onclick = "alert (returnStyle ('Demo', 'width');"> click to test </a>
</Body>
</Html>

======================================Copy codeThe Code is as follows: function getStyle (elem, name ){
// If this attribute exists in style [], it has been recently set (and is the current attribute)
If (elem. style [name])
Return elem. style [name];
// Otherwise, try IE
Else if (elem. currentStyle)
Return elem. currentStyle [name];
// Or W3C method. If so
Else if (document. defaultView & document. defaultView. getComputedStyle ){
// It uses the traditional "text-Align" style rule writing method, instead of "textAlign"
Name = name. replace (/([A-Z])/g, "-$1 ");
Name = name. toLowerCase ();
// Obtain the style object and obtain the attribute value (if any)
Var s = document. defaultView. getComputedStyle (elem ,"");
Return s & s. getPropertyValue (name );
// Otherwise, other browsers are used.
} Else
Return null;
}

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.