Get CSS style sheet style JS function currentstyle (IE), DefaultView (FF) _javascript tips

Source: Internet
Author: User
But dom.style this writing can only access the <dom style= "" ></dom&gt, so that the built-in style in the tag, if the style is written in <style type= "Text/css" ></style , or. css file, then the way to read the style.

In fact, there are other ways to read these style information, there are two ways, one is through the Document.stylesheets object, the other is through the "final style" object. In IE, this object is called Currentstyle,ff, which is called Document.defaultview. I packed the two classes and made a function to access the style information as follows:
Copy Code code as follows:

=========================== Access style sheet functions ====================================
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 object to be accessed, the type is a DOM object, or the ID of the object;

StyleName: The name of the style that needs to be accessed. The type is string, but the name cannot use the "-" number, and the same case name as the property name of the style. Object, such as Background-color to write BackgroundColor.

The function return value is of type string.

Note: This method can only access the style file and cannot be written. If you want to write a style, still use the DOM.style.XXX method. In addition, there are some style access problems under FF, such as Padding,margin. If the Padding,margin equivalent is set in the style, we can use MarginLeft to return the value.
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<style type= "Text/css" >
#demo {background-color: #000;p adding:10px;color: #fff; width:200px;}
</style>
<script type= "Text/javascript" >
=========================== Access 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>
<body>
<div id= "Demo" > here is the test content </div>
<br/><br/>
<a href= "###" onclick= "Alert" (Returnstyle (' demo ', ' width ')); > Click Test </a>
</body>

===========================
Copy Code code as follows:

function GetStyle (elem, name) {
////If the property exists in style[], it has recently been set (and is the current)
if (Elem.style[name])
return elem.style[name];
//Otherwise, try IE way
else if (elem.currentstyle)
return elem.currentstyle[name];
//or the method of the Document.defaultView.getComputedStyle, if present
else if (Document.defaultview &&) {
//it uses Traditional "text-align" style of rule writing, rather than "textAlign"
name = Name.replace (/[A-z])/g, "-$1");
name = Name.tolowercase ();
//Get the Style object and get the value of the property (if present)
var s = document.defaultView.getComputedStyle (Elem, "");
return s && s.getpropertyvalue (name);
//Otherwise, the other browser is in use
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.