jQuery的css()的底层实现就用到了getComputedStyle这个方法,也许我们用到的很少,但是不得不说这时一个非常强大的函数,下面让我们一探究竟!
第一部分:基本语法
在mdn上,我们可以看到它是这样定义的:
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.
即这个方法会返回一个元素的所有属性,无论是我们预先定义的,还是继承的或默认的。
基本语法如下所示:
var style = window.getComputedStyle(element[, pseudoElt]);
它接受两个参数,第一个是一个元素,这时必选的;第一个是伪元素(pseudo element),它是可选的
