“无法获取属性的值”appendChild':object为null或undefined“同时将脚本追加到IE [英] "Unable to get value of the property 'appendChild': object is null or undefined" while appending script to IE

查看:913
本文介绍了“无法获取属性的值”appendChild':object为null或undefined“同时将脚本追加到IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试将以下脚本添加到IE时,我收到以下错误:

When I try to append the following script to IE, I get this error:

错误:无法获取属性appendChild的值:object is null或undefined

"Error: Unable to get value of the property 'appendChild': object is null or undefined"

它在Chrome中正常工作,但是当在IE9上测试时,会发生这种情况。任何人都可以告诉我错误是什么?

It works fine in Chrome, but when testing on IE9 this occurs. Can anyone tell me what the error is?

  // 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事件。这是这样的

This can happen if your javascript code is running before all parts of HTML are loaded. Try to put this code in a function and fire it on an onload event. or use the more elegant jQuery onload event. It is something like this

$().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);

});

这篇关于“无法获取属性的值”appendChild':object为null或undefined“同时将脚本追加到IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆