【发布时间】:2012-02-16 12:20:20
【问题描述】:
我希望能够从网页上未使用的 CSS 元素获取样式。例如,这是页面:
<html>
<head>
<style type="text/css">
#custom{
background-color: #000;
}
</style>
</head>
<body>
<p>Hello world</p>
</body>
</html>
如您所见,ID 'custom' 有一个值,但未在文档中使用。我想在页面中不使用“自定义”的所有值。我最接近的是:
el = document.getElementById('custom');
var result;
var styleProp = 'background-color';
if(el.currentStyle){
result = el.currentStyle[styleProp];
}else if (window.getComputedStyle){
result = document.defaultView.getComputedStyle(el,null).getPropertyValue(styleProp);
}else{
result = "unknown";
}
【问题讨论】:
-
您尝试重新实现 CSS 类有什么特别的原因吗?请告诉我们您想要的最终结果,我们可以帮助您实现目标。
标签: javascript css styles