javascript document.head.appendChild(element) 即 ie7 和 ie8
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17100246/
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.head.appendChild(element) ie ie7 and ie8
提问by Dan
i am having an issue appending a script to the head in ie7/8
我在 ie7/8 中将脚本附加到头部时遇到问题
this is the code i am using
这是我正在使用的代码
var requireTag = document.createElement('script');
requireTag.setAttribute('type', 'text/javascript');
requireTag.setAttribute('src', link+ 'require.js');
requireTag.setAttribute('data-main', link+ 'data');
document.head.appendChild(requireTag);
this is the error i get
这是我得到的错误
SCRIPT5007: Unable to get value of the property
'appendChild': object is null or undefined
I found this createElement error in IE8and tried updating my code to have
我在 IE8 中发现了这个createElement 错误并尝试更新我的代码
var appendChild = document.head.appendChild(requireTag);
but still get the same error. Can anyone help?
但仍然得到同样的错误。任何人都可以帮忙吗?
回答by Musa
According to https://developer.mozilla.org/en-US/docs/Web/API/document.headand http://msdn.microsoft.com/en-us/library/gg593004%28v=vs.85%29.aspx, document.head
isn't available to IE<9. Just use
根据https://developer.mozilla.org/en-US/docs/Web/API/document.head和http://msdn.microsoft.com/en-us/library/gg593004%28v=vs.85% 29.aspx,document.head
不适用于 IE<9。只需使用
document.getElementsByTagName('head')[0].appendChild(requireTag);
回答by Dan
I believe document.head
isn't supported in those browsers.
我相信document.head
这些浏览器不支持。
Try this instead:
试试这个:
var head = document.getElementsByTagName("head")[0];
head.appendChild(requireTag);