给定页面的 DIV,我想通过 Javascript 获取 A 元素继承的样式表是什么。
举个例子
<html>
<head>
BODY { font-size:18px; }
A { color: red; }
#layer A { color: green; }
</head>
<div id="layer">
<a href="xxx">yyy</a>
</div>
</body>
</html>
我想要一个像 getStyle("layer", "a") 这样的 JS 函数,它返回我“font-size:18px;color: green;” ,这是应用于该 div 的该元素的所有样式,继承自所有样式表。
谢谢!
最佳答案
getComputedStyle这就是您正在寻找的。p>
我在 JSFiddle 上做了这个:http://jsfiddle.net/6MmAf/1/
<div id="layer">
<a href="xxx" id="thelink">yyy</a>
</div>
BODY { font-size:18px; }
A { color: red; }
#layer A { color: green; }
window.alert(
getComputedStyle(
document.getElementById('thelink')
).fontSize
);
// it alerts 18px, which is inherited from BODY in css
关于javascript - 通过 javascript 获取给定 div 继承的 css 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5224988/