Home  >  Article  >  Web Front-end  >  js获取元素外链样式的方法_javascript技巧

js获取元素外链样式的方法_javascript技巧

WBOY
WBOYOriginal
2016-05-16 16:17:561220browse

本文实例讲述了js获取元素外链样式的方法。分享给大家供大家参考。具体分析如下:

一般给元素设置行内样式,如

。如要获取它的样式,即可document.getElementById("div1").style.width来获取或设置。但是如果样式是在外链link中的或者是页面的非行内样式,就获取不到了。

在标准浏览器中可以通过window.getComputedStyll(obj,null)[property]来获取外链样式,但是在ie浏览器中则是通过obj.currentStyle来获取。

完整html代码如下:

复制代码 代码如下:



js获取元素外链样式


<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];//或者也可以通过window.getComputedStyle来获取样式<br /> }<br /> return null;<br /> } <p>$(document).ready(function(){<br /> $("p").click(function(){<br /> alert(getstyle(this,"width"));<br /> });<br /> });<br /> </script>



如果您点击我,弹出宽度。


点击我,弹出宽度。


也要点击我~O(∩_∩)O~。


希望本文所述对大家的javascript程序设计有所帮助。

Statement:
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