Javascript getComputedStyle文本装饰继承
getComputedStyle无法获取继承的Javascript getComputedStyle文本装饰继承,javascript,css,google-chrome,firefox,Javascript,Css,Google Chrome,Firefox,getComputedStyle无法获取继承的文本装饰属性,但可以获取字体大小 在Firefox 25和Google Chrome 30中失败 注意:在Internet Explorer 10中工作 #母公司 { 字号:38px; 文字装饰:下划线; } 测试 var elem=document.getElementById(“子”); document.write(“文本装饰:”+window.getComputedStyle(elem.getPropertyValue)(“文本装饰”);
文本装饰
属性,但可以获取字体大小
在Firefox 25和Google Chrome 30中失败
注意:在Internet Explorer 10中工作
#母公司
{
字号:38px;
文字装饰:下划线;
}
测试
var elem=document.getElementById(“子”);
document.write(“文本装饰:”+window.getComputedStyle(elem.getPropertyValue)(“文本装饰”);
文件。写(“
”);
document.write(“文本修饰:”+document.defaultView.getComputedStyle(elem.getPropertyValue)(“文本修饰”);
文件。写(“
”);
document.write(“字体大小:”+window.getComputedStyle(elem.getPropertyValue(“字体大小”));
文件。写(“
”);
document.write(“字体大小:”+document.defaultView.getComputedStyle(elem.getPropertyValue)(“字体大小”);
这是我的错误,或者浏览器失败了?
文本修饰不应该继承,即使父文本修饰影响子文本。这与继承的字体大小不同
尽管如此,这看起来确实像一个IE bug。虽然window.getComputedStyle()
在IE10中报告为继承的,但值得注意的是F12开发人员工具的说法并非如此
参考资料:
请注意,使用document.write
或document.writeln
调试代码不是一个好主意。相反,可以使用类似于console.log
-您不必处理HTML问题,您将能够更轻松地管理日志记录。感谢您的解释,但它不适用于该问题。
<!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>