0 0

如何通过jquery显示动态的HTML代码(代码中包含js方法) 5

目前在系统建设中遇到这么一个问题,比较棘手,在页面有一个div,当页面加载的时候通过jquery.html()方法动态加载一段HTML代码,其中HTML代码中含有js方法,类似于下面这个例子,通过加载这段代码后,页面会显示相应的按钮,并且点击事件也生效,但是我重新alert一下html时,只会显示【("<input type='button' value='ok' onclick='hello()'>】却没有了js方法,请大家帮我想想办法,怎么才能将html+js方法一同显示

<BODY>
    <div id="info"></div>
</BODY>
</HTML>

<SCRIPT LANGUAGE="JavaScript">
<!--
    jQuery("#info").html("<input type='button' value='ok' onclick='hello()'><script language='javascript'>function hello(){alert('hello‘);}<\/script>");
    alert(jQuery("#info").html());
//-->
</SCRIPT>

问题补充:<BODY>
    <div id="info">
<input type='button' value='ok' onclick='hello()'>
<script language='javascript'>function hello(){alert('hello‘);}<\/script>
     </div>
</BODY>
</HTML>

<SCRIPT LANGUAGE="JavaScript">
<!--
    alert(jQuery("#info").html());
//-->
</SCRIPT>

如果我在页面上这样写的话,弹出的就是html+js方法,看来jquery对静态和动态的html处理是有区别的
2010年11月01日 22:54

3个答案 按时间排序 按投票排序

0 0

采纳的答案

jquery 对html 里面含有SCRIPT会做一些处理。

创建SCRIPT对象,设置相应属性,添加到document里面。

所有你那样是获取不到的。

2010年11月08日 15:15
0 0

读入代码后JS就被插入到<body>上,你单独取那个elm是无法取出js的,因为插入的js并不属于这个elm。

2010年11月02日 17:33
0 0

    jQuery("#info").html("<input type='button' value='ok' onclick='hello()'><script language='javascript'>function hello(){alert('hello');}<\/script>"); 


alert('hello‘);
写错了

alert('hello');

2010年11月02日 09:50

相关推荐

Global site tag (gtag.js) - Google Analytics