document.body.appendChild(ⅰ)

问题描述 投票:31回答:3

我只在IE7中收到错误,因为document.body为null;当我使用Microsoft脚本编辑器进行调试时,我收到以下行中的错误:即

document.body.appendChild(ⅰ)

码:

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);

你能告诉我解决这个问题需要做些什么吗?

javascript javascript-events internet-explorer-7
3个回答
54
投票

你可以试试

document.getElementsByTagName('body')[0].appendChild(i);

如果代码在<head>中运行,并且在<body>甚至被浏览器看到之前运行,那么现在对你没有任何好处。如果您不想使用“onload”处理程序,请尝试将<script>块移动到文档的最末端而不是<head>


16
投票

这是工作。只需修改为null检查:

if(document.body!= null){document.body.appendChild(element); }

Pointy的建议很好;它可能有用,但我没试过。


2
投票

在2019年,您可以使用querySelector。

大多数浏览器都支持它(https://caniuse.com/#search=querySelector

document.querySelector('body').appendChild(i);
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.