0

I need to add a script source within a div element. For instance I have the following

<div id="recaptcha_div" class="controls">
 // script should go here
</div>

The script is as follows

<script type="text/javascript" src="https://www.google.com/recaptcha/api/challenge?k=6Lcc4OYSAAAAABTEzBumk7dNbnpKbpC3JhVdi2yU&amp;hl=en_US"></script>

How can I achieve this with JavaScript or jQuery ?

4

4 回答 4

1
var elem = document.getElementById("recaptcha_div");
var newScript = document.createElement("script");
newScript.type = "text/javascript";
newScript.src = "https://www.google.com/recaptcha/api/challenge?k=6Lcc4OYSAAAAABTEzBumk7dNbnpKbpC3JhVdi2yU&amp;hl=en_US";
elem.appendChild(newScript);

在脚本标签内关闭正文之前将此 JS 附加到您的文档中。

于 2013-09-03T05:22:55.427 回答
1

你好呀!

尝试以下操作:

document.getElementById("recaptcha_div").innerHTML="<script type='text/javascript' src='https://www.google.com/recaptcha/api/challenge?k=6Lcc4OYSAAAAABTEzBumk7dNbnpKbpC3JhVdi2yU&amp;hl=en_US'></script>"; 

祝你好运!

于 2013-09-03T05:23:29.053 回答
0

尝试这个

var ele = document.getElementById('recaptcha_div');
var js = document.createElement("script");

js.type = "text/javascript";
js.src = jsPath; // your path

ele.appendChild(js);
于 2013-09-03T05:23:51.317 回答
0

利用

$("#recaptcha_div").append("你的脚本标签");

于 2013-09-03T05:23:55.143 回答