1

我有这个 jQuery 代码:

document.getElementById('geo_us').innerHTML = "<script type='text/javascript' src='http://www.101greatgoals.com/wp-content/themes/tutorial/js/gadout.js'></scr" + "ipt>";
$("#geo_us").append("<div id='div-gpt-ad-1335877669074-0' style='width:160px; height:600px;'></div>");
document.getElementById('div-gpt-ad-1335877669074-0').innerHTML = "<script type='text/javascript' src='http://www.101greatgoals.com/wp-content/themes/tutorial/js/gad.js'></scr" + "ipt>";

这是我得到的结果:外部js的内容/代码不存在..

<div id="geo_us">
   <script src="http://www.101greatgoals.com/wp-content/themes/tutorial/js/gadout.js" type="text/javascript">
   </script>
   <div style="width: 160px; height: 600px;" id="div-gpt-ad-1335877669074-0">
      <script src="http://www.101greatgoals.com/wp-content/themes/tutorial/js/gad.js" type="text/javascript">
      </script>
   </div>
</div>

你知道代码有什么问题吗?

谢谢。

4

2 回答 2

2

您不能使用 innerHTML 添加脚本标签,请将 append 与 document.create 元素一起使用。为什么要混合使用 jQuery 和常规 JavaScript。选一个。

var str = '<script src="http://www.101greatgoals.com/wp-content/themes/tutorial/js/gadout.js"></script><div style="width: 160px; height: 600px;" id="div-gpt-ad-1335877669074-0"><script src="http://www.101greatgoals.com/wp-content/themes/tutorial/js/gad.js"></script></div>';
jQuery("#geo_us").append( str );
于 2012-06-20T14:03:23.163 回答
0

尝试这个:

$('#geo_us').html("<script type='text/javascript' src='http://www.101greatgoals.com/wp-content/themes/tutorial/js/gadout.js'></scr" + "ipt>");

$("#geo_us").append($("<div id='div-gpt-ad-1335877669074-0' style='width:160px; height:600px;'></div>"));

$('#div-gpt-ad-1335877669074-0').html("<script type='text/javascript' src='http://www.101greatgoals.com/wp-content/themes/tutorial/js/gad.js'></scr" + "ipt>");
于 2012-06-20T14:05:16.413 回答