Home > Web Front-end > JS Tutorial > body text

js method to get element external link style_javascript skills

WBOY
Release: 2016-05-16 16:17:56
Original
1260 people have browsed it

The example in this article describes the js method of obtaining the external link style of an element. Share it with everyone for your reference. The specific analysis is as follows:

Generally set inline styles for elements, such as

. If you want to get its style, you can get or set document.getElementById("div1").style.width. However, if the style is in an external link or a non-inline style of the page, it cannot be obtained.

In standard browsers, you can get the external link style through window.getComputedStyll(obj,null)[property], but in IE browser, you can get it through obj.currentStyle.

The complete html code is as follows:

Copy code The code is as follows:



js gets the external link style of the element


<script><br> function getstyle(obj,property){<br> if(obj.currentStyle){<br> return obj.currentStyle[property];<br> }else if(window.getComputedStyle){<br> return document.defaultView.getComputedStyle(obj,null)[property];//Or you can also get the style through window.getComputedStyle<br> }<br> return null;<br> } <p>$(document).ready(function(){<br> $("p").click(function(){<br> alert(getstyle(this,"width"));<br> });<br> });<br> </script>



If you click on me, the width pops up.


Click me to pop up the width.


Also click on me~O(∩_∩)O~.


I hope this article will be helpful to everyone’s JavaScript programming design.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template