原版 JavaScript 在身体上设置样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15519527/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vanilla JavaScript set style on body
提问by Hello-World
Why does this not work?
为什么这不起作用?
Using vanilla JavaScript to set the style on body tag?
使用 vanilla JavaScript 设置 body 标签的样式?
<html>
<body style="display:none;">
test
</body>
<script>
document.getElementsByTagName("body").style.display = "block";
</script>
</html>
回答by VisioN
Because getElementsByTagName()
returns a NodeList, not a single element. Treat it as array:
因为getElementsByTagName()
返回一个NodeList,而不是单个元素。将其视为数组:
document.getElementsByTagName("body")[0].style.display = "block";
Or even simpler in case of body
:
或者在以下情况下更简单body
:
document.body.style.display = "block";
回答by Aadit M Shah
If you want the body
tag you can simply use document.body
. See the demo: http://jsfiddle.net/TH3Yd/
如果你想要body
标签,你可以简单地使用document.body
. 看演示:http: //jsfiddle.net/TH3Yd/
回答by Darius Miliauskas
I used the following solution:
我使用了以下解决方案:
<script>
function changeDisplay(){
document.body.style.display = 'block';
}
setTimeout(function () { changeDisplay(); }, 0);
</script>