标签:伪类 height 匹配 value should mil purpose 版本 href
Description
The returned object is of the same type that the object returned from the element‘s style property, however the two objects have different purpose. The object returned from getComputedStyle is read-only and can be used to inspect the element‘s style (including those set by a <style> element or an external stylesheet). The elt.style object should be used to set styles on a specific element.
这段话只是说明getComputedStyle获取的值是只读的并且可被用于检测元素的样式(包括style属性和外部样式).而elt.style可被用于设置指定元素的样式.
以chrome为例查看getComputedStyle()与元素的style属性的区别,请注意其中cssText属性的区别
<a href="#" id="showAndHide">show and hide</a>
<script type="text/javascript" src="./jquery-1.8b1.js"></script>
<script>
$(‘#showAndHide‘).click(function() {
var dom = this;
console.log(getComputedStyle( dom ));
console.log( dom.style );
});
</script>
defaultView
在很多在线的domo中,getComputedStyle总是被当作document.defaultView的方法使用,其实这不是必需的. 因为getComputedStyle是存在于window对象. 使用Firefox 3.6访问框架样式是唯一一处必须使用defaultView的地方.
获取伪元素(如:after, :before, :marker, :line-marker)的样式
直接给出官方的Demo:
<style>
h3:after {
content: ‘ rocks!‘;
}
</style>
<h3>generated content</h3>
<script>
var h3 = document.querySelector(‘h3‘),
result = getComputedStyle(h3, ‘:after‘).content;
console.log(‘the generated content is: ‘, result); // returns ‘ rocks!‘
</script>
window.getComputedStyle可获取 伪类元素 样式
标签:伪类 height 匹配 value should mil purpose 版本 href
原文地址:http://www.cnblogs.com/hehuiself/p/7100185.html