getcomputedstyle和style的区别

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了getcomputedstyle和style的区别相关的知识,希望对你有一定的参考价值。

1.只读与可写
getComputedStyle方法是只读的,只能获取样式,不能设置;而element.style能读能写,能屈能伸。
2.获取的对象范围
getComputedStyle方法获取的是最终应用在元素上的所有CSS属性对象(即使没有CSS代码,也会把默认的祖宗八代都显示出来);而element.style只能获取元素style属性中的CSS样式。因此对于一个光秃秃的元素<p>,getComputedStyle方法返回对象中length属性值(如果有)就是190+(据我测试FF:192, IE9:195, Chrome:253, 不同环境结果可能有差异), 而element.style就是0。
3.作用
getComputedStyle方法有一个很重要的,类似css()方法没有的功能——获取伪类元素样式
4.兼容性
getComputedStyle方法IE6~8是不支持的

以上是关于getcomputedstyle和style的区别的主要内容,如果未能解决你的问题,请参考以下文章

js中style,currentStyle和getComputedStyle的区别

style currentStyle getComputedStyle的区别和用法

js中getComputedStyle()与currentStyle()style()方法的区别

js中getComputedStyle()与currentStyle()style()方法的区别

js中style,currentStyle和getComputedStyle的区别以及获取css操作方法

获取css属性的style getComputedStyle currentStyle三种方法的区别总结