我在 IE 中使用此代码时遇到问题(使用 Chrome 似乎工作正常):
<html>
<body>
<script type="text/javascript">
var scriptContent = "var whatever=1";
var _js = document.createElement('script');
_js.setAttribute('type', 'text/javascript');
textNode = document.createTextNode(scriptContent);
_js.appendChild(textNode);
document.getElementsByTagName('body')[0].appendChild(_js);
</script>
</body>
</html>
我在 Internet Explorer (IE9) 中遇到的错误是:“意外调用方法或访问属性”语句“_js.appendChild(textNode)”。
有什么办法可以解决这个问题吗?
最佳答案
如你所见here appendChild()
在 IE 中不适用于 <script>
-元素。
(好像IE9支持,不过要看浏览器模式)
Nivas之前有一个正确答案, 不幸的是它已经被删除了。 在IE中使用
_js.text = scriptContent;
关于javascript - 在 Javascript 中使用 appendChild 和 IE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7090198/