为什么我不能在IE中向innerHTML添加包含脚本标记的字符串 [英] Why can't I add a string containing a script tag to innerHTML in IE

查看:139
本文介绍了为什么我不能在IE中向innerHTML添加包含脚本标记的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作(我正在使用原型库):

I'm trying to do the following (I'm using the prototype library):

var div = document.createElement('div');
div.innerHTML = '<script src="somescript.js"></script>';
$('banner').insert(div);

在IE中,div.innerHTML属性在第二次设置属性后总是等于 line。

In IE, div.innerHTML property is always equal to "" after I set the property in the second line.

这个代码片段在一个函数中,它覆盖了外部供应商脚本中的document.write(),这就是为什么我这样做而不是创建一个脚本元素,并直接将它附加到div元素。

This snippet is inside a function which is overriding document.write() in an external vendor script, so that is why I am doing it this way rather than creating a script element and appending it to the div element directly.

任何帮助都会非常感激,这给了我白发!

Any help would really be appreciated, this is giving me grey hairs!

推荐答案

您可以尝试这样做:

function loadScript(src) {
       var script = document.createElement("script");
       script.type = "text/javascript";
       document.getElementsByTagName("head")[0].appendChild(script);
       script.src = src;
}

或者

..
div.innerHTML = "<script src=\"somescript.js\"></script>";
..

这篇关于为什么我不能在IE中向innerHTML添加包含脚本标记的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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