任何支持style特性的HTML元素在JavaScript中都有一个对应的style属性。这个style对象是CSSStyleDeclaration的实例,包含着通过HTML的style特性指定的所有样式信息,但不包含于外部样式表或嵌入样式表经层叠而来的样式。
HTML:
<div class="error" >
</div>
<div class="right" style="height:100px">
</div>
CSS:(引用外部CSS文件或在<head>标签中)
<style>
.error{
height:100px;
}
</style>
JS:
var div1=document.querySelector(".error");
var div2=document.querySelector(".right");
console.log(div1.style.height);//null
console.log(div2.style.height);//100px