1

I create a dynamical tag with id "video1" inside the static tag with id "videos":

document.getElementById("videos").innerHTML = '<div id="video1"></div>';

But when I want to append another script tag, I failed.

The following is my code to append that tag:

var s = document.createElement("script");
s.id = "scriptId"; //script tag's id
document.getElementById("video1").appendChild(s);

The above code works for static tag, but it doesn't work for the dynamical generated tag video1. So is there any way I can append a script tag to an another tag which is also dynamically generated by javascript?

Thanks.

4

1 回答 1

0

相反,将其附加到头部:

var s = document.createElement("script");
s.id = "scriptId"; //script tag's id
document.getElementsByTagName("head")[0].appendChild(s);

您还可以使用eval(s.text)来评估脚本的文本内容

于 2012-12-22T20:59:16.930 回答