This article illustrates the method of JS acquiring element outer chain style. Share to everyone for your reference. The specific analysis is as follows:
Generally set inline styles for elements, such as <div id= "Div1" style= "width:500px;" ></div>. To get its style, you can document.getElementById ("Div1"). Style.width to get or set. However, if the style is in the outside link or the page is not inline style, you can not get.
In standard browsers, the Window.getcomputedstyll (obj,null) [property] can be used to get the outer chain style, but in IE browsers it is obtained by Obj.currentstyle.
The complete HTML code is as follows:
Copy Code code as follows:
<! DOCTYPE html>
<title>js get element outer chain style </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];//or can also get the style through window.getComputedStyle
}
return null;
}
$ (document). Ready (function () {
$ ("P"). Click (function () {
Alert (GetStyle (This, "width"));
});
});
</script>
<body>
<p style= "width:750px;" > If you click me, eject the width. </p>
<p> Click me to eject the width. </p>
<p> also want to click on my ~o (∩_∩) o~. </p>
</body>
I hope this article will help you with your JavaScript programming.