Js is used to obtain the element external link style. js is used to obtain the element style.

Source: Internet
Author: User

Js is used to obtain the element external link style. js is used to obtain the element style.

This article describes how to obtain the element external link style by js. Share it with you for your reference. The specific analysis is as follows:

Set the row style for the element, such as <div id = "div1" style = "width: 500px;"> </div>. To get its style, you can get or set it by document. getElementById ("div1"). style. width. However, if the style is in the outer link or the non-row style of the page, it cannot be obtained.

In the standard browser, you can use window. getComputedStyll (obj, null) [property] to obtain the external link style, but in IE, you can use obj. currentStyle.

The complete html code is as follows:
Copy codeThe Code is as follows: <! DOCTYPE html>
<Html>
<Head>
<Title> js get element external link styles </title> <base target = "_ blank"/>
<Style type = "text/css">
P {
Width: 500px;
Line-height: 30px;
}
</Style>
<Script src = "jquery/jquery-1.11.2.min.js">
</Script>
<Script>
Function getstyle (obj, property ){
If (obj. currentStyle ){
Return obj. currentStyle [property];
} Else if (window. getComputedStyle ){
Return document. defaultView. getComputedStyle (obj, null) [property]; // you can also use window. getComputedStyle to obtain the style.
}
Return null;
}

$ (Document). ready (function (){
$ ("P"). click (function (){
Alert (getstyle (this, "width "));
});
});
</Script>

</Head>
<Body>
<P style = "width: 750px;"> if you click "I", the width is displayed. </P>
<P> click "I" to display the width. </P>
<P> click me ~ O (worker _ worker) O ~. </P>

</Body>
</Html>

I hope this article will help you design javascript programs.

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.