Javascript document.body.appendChild(i)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5419733/
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
document.body.appendChild(i)
提问by san
I am getting error only in IE7 as document.body
is null; when I debug with Microsoft script editor I am getting the error in the following line:
i.e.
我只在 IE7 中遇到错误,因为它document.body
是空的;当我使用 Microsoft 脚本编辑器进行调试时,出现以下行中的错误:即
document.body.appendChild(i)
document.body.appendChild(i)
Code:
代码:
function nm_eraseCookie(name){
nm_createCookie(name,"",-1)
}
var i=document.createElement('IMG');
i.src='//e.netmng.com/pixel/?aid=403';
i.width=1;
i.height=1;
document.body.appendChild(i);
nm_createCookie('nm_belgacom_bt',
escape('tv1=bun_intvtel;tv2=;tv3=;phone1=hbs_discoveryline;phone2=hbs_classical_line;phone3=;inet1=bun_nettvmob;inet2=hbs_adsl_res_plus;inet3=hbs_adsl_res_go;nm_banner=;nm_popin=hbs_discoveryline;'),183);
Can you inform me what I need to do to solve this issue?
你能告诉我我需要做什么来解决这个问题吗?
采纳答案by san
It is working. Just modify to null check:
这是工作。只需修改为空检查:
if(document.body != null){
document.body.appendChild(element);
}
Pointy's suggestion is good; it may work, but I didn't try.
Pointy的建议很好;它可能有效,但我没有尝试。
回答by Pointy
You could try
你可以试试
document.getElementsByTagName('body')[0].appendChild(i);
Now that won't do you any good if the code is running in the <head>
, and running beforethe <body>
has even been seen by the browser. If you don't want to mess with "onload" handlers, try moving your <script>
block to the very end of the document instead of the <head>
.
现在,如果代码在运行,不会给你带来任何好<head>
和运行之前的<body>
,甚至被通过浏览器看到的。如果您不想弄乱“onload”处理程序,请尝试将<script>
块移动到文档的末尾而不是<head>
.
回答by Konrad Ga??zowski
In 2019 you can use querySelector for that.
在 2019 年,您可以为此使用 querySelector。
It's supported by most browsers (https://caniuse.com/#search=querySelector)
大多数浏览器都支持它 ( https://caniuse.com/#search=querySelector)
document.querySelector('body').appendChild(i);
回答by Utkarsh Dhiman
You can appendChild
to document.body
but not if the document hasn't been loaded. So you should
put everything in:
您可以appendChild
到document.body
但如果该文件尚未加载。所以你应该把所有东西都放进去:
window.onload=function(){
//your code
}
This works or you can make appendChild
to be dependent on something else like another event for eg.
这有效,或者您可以appendChild
依赖于其他事件,例如另一个事件。
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_doc_body_append
https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_doc_body_append
As a matter of fact you can try changing the innerHTML
of the document.body
it works...!
作为事实上,你可以尝试改变innerHTML
的document.body
它的工作原理...!
回答by bob
May also want to use "documentElement":
可能还想使用“documentElement”:
var elem = document.createElement("div");
elem.style = "width:100px;height:100px;position:relative;background:#FF0000;";
document.documentElement.appendChild(elem);
回答by Vy Le
If your script is inside head tag in html file, try to put it inside body tag. CreateElement while script is inside head tag will give you a null warning
如果您的脚本位于 html 文件的 head 标签内,请尝试将其放入 body 标签内。CreateElement 当脚本在 head 标签内时会给你一个空警告
<head>
<title></title>
</head>
<body>
<h1>Game</h1>
<script type="text/javascript" src="script.js"></script>
</body>