函数获取非行间样式兼容写法 obj.currentStyle[attr]和getComputedStyle(obj, false)[attr]
#div1 {200px; height:200px; background:red;} <script> function getStyle(obj, name) { if(obj.currentStyle) { return obj.currentStyle[name]; } else { return getComputedStyle(obj, false)[name]; } } window.onload=function () { var oDiv=document.getElementById('div1'); alert(getStyle(oDiv, 'backgroundColor'));//background、border属于复合样式需要写成backgroundColor, borderWidth }; </script> <div id="div1"></div>