- node.style.属性名
【这种方式只能获取行间样式,写在CSS选择器中的样式获取不到】<style> div { background-color: orange; } </style> <body> <div id="box" style="width: 100px; height: 200px;"></div> </body> <script> var oDiv = document.getElementById('box'); console.log(oDiv.style.width); // '100px' console.log(oDiv.style.height); // '100px' console.log(oDiv.style.backgroundColor); // null </script>
- node.currentStyle[‘属性名’]
【兼容IE】 - getComputedStyle(node)[‘属性名’]
【兼容Firefox、chrome】 - 封装函数:全兼容
function getCurrentStyle(node, attrName) { return node.currentStyle ? node.currentStyle[attrName] : getComputedStyle(node)[attrName]; }
JS获取节点的有效样式
最新推荐文章于 2023-02-01 11:28:59 发布