在Javascript中使用appendChild和IE [英] Using appendChild with IE in Javascript

查看:195
本文介绍了在Javascript中使用appendChild和IE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在IE中使用此代码遇到问题(使用Chrome似乎工作正常):

I am having trouble with this code in IE (with Chrome it seems to work fine):

<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)中遇到的错误是:意外调用方法或访问属性on statement_js.appendChild(textNode)。

The error I get in Internet Explorer (IE9) is: "unexpected call to a method or access to a property" on statement "_js.appendChild(textNode)".

有没有办法解决这个问题?

Is there any way to work around this problem?

推荐答案

正如您所见此处 IE中的appendChild()未应用于< script> -elements。
(似乎IE9支持它,但它取决于浏览器模式)

As you can see here appendChild() in IE is not applied to <script>-elements. (Seems as if IE9 supports it, but it depends on the browser-mode)

之前有一个正确答案Nivas ,遗憾的是它已被删除。
在IE中使用

There was an correct answer before by Nivas, unfortunately it has been deleted. In IE use

_js.text = scriptContent; 

这篇关于在Javascript中使用appendChild和IE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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