本文介绍了一种在JavaScript中获取页面引用CSS样式表属性值的通用方法。该方法兼容不同浏览器,通过判断node.style、currentStyle以及getComputedStyle来准确获取元素样式,适用于需要动态读取或修改CSS属性的场景。
JavaScript获取CSS样式表属性值的方法
具体实现代码如下所示:
function getStyle(node, property){
if (node.style[property]) {
return node.style[property];
}
else if (node.currentStyle) {
return node.currentStyle[property];
}
else if (document.defaultView && document.defaultView.getComputedStyle) {
var style = document.defaultView.getComputedStyle(node, null);
return style.getPropertyValue(property);
}
return null;
}
上述内容即为JavaScript获取页面中CSS样式表属性值的推荐实现方式,供各位开发者参考使用,欢迎持续关注本站获取更多技术分享。