脚本在jQuery中不起作用

时间:2012-06-20 14:02:11

标签: javascript jquery html json

我有这个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>

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

感谢。

2 个答案:

答案 0 :(得分:2)

您无法使用innerHTML添加脚本标记,请使用append with 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 );

答案 1 :(得分: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>");