window.getComputedStyle(div, null)
<script> ???var div = document.getElementsByTagName("div")[0]; ???console.log(typeof ?window.getComputedStyle(div, null));//object ???console.log(window.getComputedStyle(div, null).width);//400px ???console.log(window.getComputedStyle(div, null)["width"]);//400px ???console.log(window.getComputedStyle(div, null)["background-color"]);//rgb(255, 192, 203) ???console.log(window.getComputedStyle(div, null).backgroundColor);//rgb(255, 192, 203)</script>
div.currentStyle(IE678)
<script> ???var div = document.getElementsByTagName("div")[0]; ???console.log(div.currentStyle["width"]); ???console.log(div.currentStyle.width);</script>
兼容写法
<script> ???var div = document.getElementsByTagName("div")[0]; ???console.log(getStyle(div, "width")); ???console.log(getStyle(div, "backgroundColor")); ???function getStyle(ele, attr) { ???????if (window.getComputedStyle) { ???????????return window.getComputedStyle(ele, null)[attr]; ???????} ???????return ele.currentStyle[attr]; ???}</script>
注意事项:普通获取属性方式div.style.width或者div.style["width"]无法获取内嵌和外链式,只能获取行内式
JS——获取属性
原文地址:http://www.cnblogs.com/wuqiuxue/p/7988485.html