javascript - document.head.appendChild(element) 即 ie7 和 ie8

标签 javascript appendchild

我在 ie7/8 中将脚本附加到头部时遇到问题

这是我正在使用的代码

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

这是我得到的错误
SCRIPT5007: Unable to get value of the property
'appendChild': object is null or undefined  

我找到了 createElement error in IE8并尝试更新我的代码
var appendChild = document.head.appendChild(requireTag);

但仍然得到同样的错误。任何人都可以帮忙吗?

最佳答案

根据https://developer.mozilla.org/en-US/docs/Web/API/document.headhttp://msdn.microsoft.com/en-us/library/gg593004%28v=vs.85%29.aspx , document.head不适用于 IE<9。只需使用

document.getElementsByTagName('head')[0].appendChild(requireTag);

关于javascript - document.head.appendChild(element) 即 ie7 和 ie8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17100246/

相关文章:

javascript setInterval() 和变量作用域

javascript - AngularJS ngIf 阻止在指令中查找元素

javascript - 无法从 xpage 上选项卡式面板中的编辑框中读取值

javascript - 使用 createDocumentFragment 插入嵌套的 div 结构

javascript - 如何遍历内部 HTML

javascript - 将 async/await 与 promise 和 appendChild 结合使用时,应用程序停止工作

javascript - 单击 JavaScript 更改按钮颜色

Javascript 进度条没有更新 'on the fly' ,但是一旦进程完成就立即更新?

JavaScript -> 未捕获类型错误 : Failed to execute 'appendChild' on 'Node'

javascript - 通过 Onclick 使用 appendChild/replaceChild 函数切换图像堆栈