index.html
<div id="box"></div>
index.css
#box{
width: 50px;
height: 50px;
background: red;
}
js:
window.onload = function(){
console.log(document.getElementById("box").style.backgroundColor);
}
最后输出为空。这里为什么获取不到DOM的style属性呢?
document.getElementById("box").style获取的是元素行间设置的样式,不能获取到css中设置的样式。如果要获取css中设置的样式,可以试试getComputedStyle(标准浏览器)或者currentStyle(ie低版本)