你们应该很是熟悉jQuery的css()方法,那么如何在不引用jQuery的状况下一样实现这个功能呢?本文就介绍使用原生JS来获取样式的方法.css
做者:Icarus
原文连接:咱们来翻翻元素样式的族谱-getComputedStylehtml
The Window.getComputedStyle() method gives the values of all the CSS properties of an element after applying the active stylesheets and resolving any basic computation those values may contain.git
Window.getComputedStyle()方法能够获取当前元素全部最终使用的CSS属性值.返回的是一个CSS样式声明对象(object CSSStyleDeclaration),只读.也就是说,获取到的不只仅是咱们自定义的样式,它包含了全部做用在当前元素上的css属性及属性值.github
var style = window.getComputedStyle(element[, pseudoElt]);
其中element是必需的参数,表明获取样式的元素.pseudoElt是伪类参数,在Gecko2.0以前是必填项,但在现代浏览已经不是了,若是不是伪类的话,设置为null便可.chrome
var style = window.getComputedStyle(element, null);
假设页面上存在一个id为id的元素,使用getComputedStyle方法获取到的元素样式以下所示:浏览器
<style> h3::after { content: 'rocks!'; } </style> <h3>generated content</h3> <script> var h3 = document.querySelector('h3'), result = getComputedStyle(h3, ':after').content; console.log(result); // returns 'rocks!' </script>
其中问号部分表明暂无测试,是否兼容暂不肯定.app
由上图可知,getComputedStyle的兼容性很不错,基本支持全部的现代浏览器.固然IE浏览器自有他的脾气,在IE9如下有另外一套功能类似的API,暂且不提.框架
在上面的栗子中,咱们能够看到getComputedStyle返回的是样式声明对象,包含了元素全部的样式值,那么咱们如何获取到想要的属性值呢?有两种方法能够实现这一需求:less
window.getPropertyValue()wordpress
键值访问
getPropertyValue方法能够直接获取CSS样式申明对象上的属性值,例如:
window.getComputedStyle(element, null).getPropertyValue('属性名');
能够很是方便的获取到咱们想要的属性值.须要注意:不支持驼峰命名,属性名按照css的写法,如background-color
:
window.getComputedStyle(element, null).getPropertyValue('background-color');
除IE9如下浏览器,其他现代浏览器均支持.
经过键值访问来获取css属性较为繁琐,可能须要进行额外的浏览器检测,例如
window.getComputedStyle(element, null).float //错误!
这种写法是错误的,缘由是float是js的一个保留字,不能直接使用.IE下对应的是 styleFloat,firefox,chorme,safari下对应的则是cssFloat.相较而言更建议使用getPropertyValue来获取具体属性值.
currentStyle是IE浏览器特有的的一个属性,element.currentStyle
返回的一样是全部元素当前应用的最终CSS属性值.可是其中获取到的属性名会存在差别,如上说起的styleFloat和cssFloat.
不过,currentStyle属性不支持伪类样式获取,这是与getComputedStyle方法的重要差别,也是jQuery中css()方法没法获取伪类元素属性的缘由.
假设页面上有一个id为test的元素,示例以下:
var style = document.getElementById('test').currentStyle;
在IE浏览器中的getAttribute方法提供了与getPropertyValue方法相似的功能,配合currentStyle使用,能够访问CSS样式对象的属性,用法与getPropertyValue相似:
element.currentStyle.getAttribute('float');
能够注意到,使用getAttribute一样不须要进行浏览器检测.可是有一点须要注意:在IE7+的浏览器中,getAttribute获取属性名可使用驼峰式命名法,IE6必须使用驼峰式命名方法,如:
// IE7,8二者都可,IE6必须使用驼峰命名法 element.currentStyle.getAttribute('background-color'); element.currentStyle.getAttribute('backgroundColor');
咱们使用element.style也能够获取元素的CSS样式声明对象,可是其与getComputedStyle方法存在一些差别.
上面提到过getComputedStyle方法是只读的,只能获取样式,不能设置;而element.style能读能写,八面玲珑.
getComputedStyle方法获取的是最终应用在元素上的全部CSS属性对象,即便没有编写任何样式代码,也会获取默认的全部样式的属性和属性值;element.style
只能获取元素style属性中的CSS样式.
可能这样说不太好理解,咱们回顾一下CSS样式表的表现形式:
内联样式 (inline Style): 是写在HTML标签的style属性里面的,内嵌样式只对该标签有效.
内部样式 (internal Style Sheet): 是写在HTML文档的style标签里面的,内部样式只对当前页面有效.
外部样式表 (External Style Sheet): 若是不少网页须要用到一样的样式,将样式写在一个以.CSS为后缀的CSS文件里,而后在每一个须要用到这些样式的网页里引用这个CSS文件.也就是说,getComputedStyle获取到的是全部最终在元素上应用的样式属性,而element.style
仅仅获取的是咱们人为编写的样式.
咱们来作一个测试,对于一个光秃秃的没有任何样式设置的元素p,getComputedStyle方法返回对象中length属性值和element.style
的区别.
<p></p> var elem = document.querySelector('p'); // 0 elem.style.length // 261 - chrome 55.0.2883.87 // 249 - firefox 50.0 // 233 - safari 5.1.1 window.getComputedStyle(elem, null).length
很容易看出二者的区别.
From mdn
In many code samples online, getComputedStyle is used from the document.defaultView object. In nearly all cases, this is needless, as getComputedStyle exists on the window object as well. It's likely the defaultView pattern was some combination of (1) folks not wanting to write a spec for window and (2) making an API that was also usable in Java. However, there is a single case where the defaultView's method must be used: when using Firefox 3.6 to access framed styles.
window.getComputedStyle
还有另外一种写法,就是 document.defaultView.getComputedStyle
.
实际上,使用defaultView基本上是没有必要的,getComputedStyle自己就存在window对象之中.使用defaultView可能一是人们不太乐意在window上专门写个东西,二是让API在Java中也可用.
不过有个特殊状况,在FireFox3.6上不使用defaultView方法就搞不定的,就是访问框架(frame)的样式.不过FireFox3.6已经退出历史舞台,不用过于在乎.
element.style
可读可写,但只能获取到自定义style属性
window.getComputedStyle
/document.defaultView.getComputedStyle
只读,非IE浏览器及IE9+获取全部做用样式,使用getPropertyValue
来获取特定属性.
currentStyle
只读,IE6-8获取全部做用样式,使用getAttribute
来获取特定属性.
这篇博客主要介绍了getComputedStyle的前世此生,真正要实现jQuery中兼容IE及其它现代浏览器的css()方法还须要额外作一些兼容性的处理.限于篇幅,欲知后事如何,且听下回分解.