无法获取属性值'appendChild':对象为空或未定义"同时追加脚本到IE

"Unable to get value of the property 'appendChild': object is null or undefined" while appending script to IE

本文关键字:quot 未定义 IE 追加 脚本 对象 属性 获取 appendChild      更新时间:2023-09-26

当我尝试将以下脚本附加到IE时,我得到这个错误:

"错误:无法获取属性'appendChild'的值:对象为空或未定义"

它在Chrome中工作良好,但在IE9上测试时发生了这种情况。有人能告诉我错误是什么吗?

  // create script in document
    var fbScript = document.createElement("script");
    fbScript.type = "text/javascript";
    // make script source the facebook plugin
    fbScript.src = "http://connect.facebook.net/en_US/all.js#xfbml=1";

    // append script to appropriate tab
    document.getElementById('Tab' + tab_counter).appendChild(fbScript);

如果您的javascript代码在加载HTML的所有部分之前运行,则可能发生这种情况。尝试将此代码放入函数中,并在onload事件时触发它。或者使用更优雅的jQuery onload事件。大概是这样的

$().ready(function () {
    var fbScript = document.createElement("script");
    fbScript.type = "text/javascript";
    // Make script source the facebook plugin
    fbScript.src = "http://connect.facebook.net/en_US/all.js#xfbml=1";
    // Append script to appropriate tab
    document.getElementById("Tab" + tab_counter).appendChild(fbScript);
});