javascript - 尝试调用 appendChild 时如何处理 document.body 在 IE7 上为 null

标签 javascript internet-explorer-7

由于 document.body 在该平台上为空,我收到特定于 Internet Explorer 7 的错误。

当我尝试在以下代码中执行 document.body.appendChild(i) 时发生错误:

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

我该如何解决这个问题?

最佳答案

你可以试试

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

现在,如果代码在 <head> 中运行,那对您没有任何好处。 ,并在 <body> 之前运行甚至已经被浏览器看到了。如果您不想弄乱“onload”处理程序,请尝试移动您的 <script> block 到文档的最后而不是 <head> .

关于javascript - 尝试调用 appendChild 时如何处理 document.body 在 IE7 上为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5419733/

相关文章:

html - 如何将相对路径添加到 PIE.htc 文件?

html - 表格在 IE7 中无法正确调整大小

jQuery .replaceWith() 替代

javascript - 从列/数据名称中单击 C3Js 设置参数

javascript - 类型错误:无法在简单的 JavaScript 游戏中读取未定义的属性 '#'

javascript - IE7 javascript 错误 : runtime-error microsoft jscript: dropdownlist is not defined

jquery - 为什么 Jquery .attr() 对 IE 7/8 中的我的对象不起作用?

javascript - 在html5中,我可以强制浏览器下载我正在使用的音频文件吗?

javascript - 将点击事件添加到 React.Js

javascript - 如何在显示对话框时暂时禁用网页滚动?