This article example describes how to obtain the external link style of js. Share it for your reference. The specific analysis is as follows:
Generally, set inline styles for elements, such as <div id="div1"></div>. To get its style, you can get or set it by document.getElementById("div1").style.width. However, if the style is in the external link or the non-line style of the page, it cannot be obtained.
In a standard browser, you can use window.getComputedStyll(obj,null)[property] to get the external link style, but in an ie browser, you can use obj.currentStyle to get it.
The complete html code is as follows:
Copy the code as follows: <!DOCTYPE html>
<html>
<head>
<title>js get element external link 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 you can also get the style through window.getComputedStyle
}
return null;
}
$(document).ready(function(){
$("p").click(function(){
alert(getstyle(this,"width"));
});
});
</script>
</head>
<body>
<p>If you click me, the width pops up. </p>
<p>Click me to pop up the width. </p>
<p>Click me too~O(∩_∩)O~. </p>
</body>
</html>
I hope this article will be helpful to everyone's JavaScript programming.