getComputedStyle获取text-decoration
属性继承失败,但可以获取font-size
。
在 Firefox 25 和 GoogleChrome 30 中失败。
Note: In Internet Explorer 10 work!
<!DOCTYPE html>
<html>
<style>
#parent
{
font-size: 38px;
text-decoration: underline;
}
</style>
<body>
<div id="parent">
<p id="child">Test</p>
</div>
<script>
var elem = document.getElementById("child");
document.write("text-decoration:"+window.getComputedStyle(elem).getPropertyValue("text-decoration"));
document.write("<br>");
document.write("text-decoration:"+document.defaultView.getComputedStyle(elem).getPropertyValue("text-decoration"));
document.write("<hr>");
document.write("font-size:"+window.getComputedStyle(elem).getPropertyValue("font-size"));
document.write("<br>");
document.write("font-size:"+document.defaultView.getComputedStyle(elem).getPropertyValue("font-size"));
</script>
</body>
</html>
是我的错,还是浏览器的问题?
最佳答案
text-decoration
不应继承,即使父文本装饰会影响子文本。这与继承的 font-size
不同。
话虽如此,这看起来确实像一个 IE 错误。虽然 window.getComputedStyle()
在 IE10 中报告为继承,但值得注意的是 F12 开发人员工具另有说明。
引用资料:
https://developer.mozilla.org/en-US/docs/Web/CSS/text-decoration
关于javascript - getComputedStyle 文本修饰继承,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19750436/