|
阅读:7363回复:0
style属性与currentStyle属性及getComputedStyle属性
1. 使用style属性(只能获取内联样式属性值),特点:可读,可写,只能操作内联模式
语法: HTML元素.style.样式属性 = “值”; 注:如果样式属性名称中有带有"-"号,去掉“-”,并且首字母大写 扩展:此方法只能获取内联样式,内样样式与外部样式无法获取,那如何获取呢,可以使用currentStyle对象(只限于IE) 语法: HTML元素.currentStyle.样式属性; DOM提供了getComputedStyle()方法(IE不支持,Firefox支持) 语法:document.defaultView.getComputedStyle(元素,null).属性; 注: 第二个参数表示伪元素,如:hover; 另:getComputedStyle可以发现有两种调用方式,如下: window.getComputedStyle() ,与document.defaultView.getComputedStyle() 两种方式,有什么区别,我们应该如何使用:具说是,在FireFox3.6上不使用defaultView方法就搞不定的,就是访问框架(frame)的样式.,本人没有进行测试。不过jquery框架里可以是使用的defaultView的方式,所有,写defaultView方式,应该是兼容性更好吧。 所以,最好还是使用document.defaultView.getComputedStyle()的方式来获取样式。 可参考以下文章:http://www.zhangxinxu.com/wordpress/2012/05/getcomputedstyle-js-getpropertyvalue-currentstyle/ 注:currentStyle与getComputedStyle只能读取样式,可读取元素的最终应用样式 两者的兼容性写法: 元素.currentStyle? 元素.currentStyle : document.defaultView.getComputedStyle(元素, null); |
|
|