style、getComputedStyle与currentStyle

css三种样式

1.内嵌样式(inline Style) :是写在Tag里面的,内嵌样式只对所有的Tag有效。

2.内部样式(internal Style Sheet):是写在HTML的里面的,内部样式只对所在的网页有效。

3.外部样式表(External Style Sheet):如果很多网页需要用到同样的样式(Styles),将样式(Styles)写在一个以.css为后缀的CSS文件里,然后在每个需要用到这些样式(Styles)的网页里引用这个CSS文件。

style

最常用的是style属性,在JavaScript中,通过document.getElementById(id).style.XXX就可以获取到XXX的值,但意外的是,这样做只能取到通过内嵌方式设置的样式值,即style属性里面设置的值。
所有的css属性 都可以用e.style.属性访问
但只适合设置样式,不适合查询样式

so…
通过currentStyle or etComputedStyle可以获取到通过内联或外部引用的CSS样式的值

getComputedStyle(FF,chrome…)

计算样式只读,确切地决定了浏览器在渲染时使用的样式属性,

《style、getComputedStyle与currentStyle》 计算样式
《style、getComputedStyle与currentStyle》 getComputedStyle

currentStyle(IE)

getComputedStyle()接受两个参数:要取得计算样式的元素和一个伪元素,如果不需要伪元素,则可以是null。然而,在IE中,并不支持getComputedStyle,IE提供了currentStyle属性 。

《style、getComputedStyle与currentStyle》 currentStyle

举个栗子

《style、getComputedStyle与currentStyle》 用法实例

设置、查询css属性的其他方式

setAttribute cssText

《style、getComputedStyle与currentStyle》 Paste_Image.png

test

《style、getComputedStyle与currentStyle》 test

参考style、currentStyle、getComputedStyle区别介绍

    原文作者:玄月府的小妖在debug
    原文地址: https://www.jianshu.com/p/851881d2762e
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞