Home  >  Article  >  Web Front-end  >  JS获取对象的某个CSS属性值的问题_html/css_WEB-ITnose

JS获取对象的某个CSS属性值的问题_html/css_WEB-ITnose

WBOY
WBOYOriginal
2016-06-21 08:56:011220browse

function getStyle(obj,attr){ if(obj.currentStyle){ return obj.currentStyle[attr]; }else{ return getComputedStyle(obj,false)[attr]; } }

该方法很强大,兼容所有浏览器,可获取对象的某个特定的CSS属性;

但是后来使用该方法时,发现在火狐和IE上有个共同的BUG,就是获取border的值时,他们都返回空了;

因为浏览器对border的渲染有差异,在火狐上border是分四个方向来渲染的,

所以获取border的值时,要分开获取,

这样:getStyle(obj,'borderLeftWidth') 来取得边框的值。

其实这个方法本身是没有任何问题的,只是在使用时,只是在使用时对浏览器本身的一些差异,还是要注意;

是否其他属性的值也有这种差异,还要在实际使用时去发现。

另外,该方法在获取行内样式时是不通用的,还是要用 obj.style[attr]

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