首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > JavaScript >

关于jquery toggle函数的疑点

2013-04-02 
关于jquery toggle函数的疑问?!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN http://

关于jquery toggle函数的疑问?

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">

<script src="jquery-1.4.4.js"></script>
<script type="text/javascript">
$(function(){
$("#change").toggle(function(){
$("#text2").append("<h2>添加文本</h2>");
},function(){
$("#text2").remove();
});
});
</script>

 </head>

 <body>
<input type="button" value="change" id="change"/>
<p id="text2"></p>
 </body>
</html>




点击按钮后,前两次有反应。点第三次就没有反应,不是说toggle是循环执行吗?? jquery??,toggle
[解决办法]
remove方法:
从DOM中删除所有匹配的元素。

这个方法不会把匹配的元素从jQuery对象中删除,因而可以在将来再使用这些匹配的元素。但除了这个元素本身得以保留之外,其他的比如绑定的事件,附加的数据等都会被移除。

事件还是在循环,只是元素上的事件被删除了
 

 $(function(){        
$("#change").toggle(function(){  
alert($('#text2'))
//$("#text2").append("<h2>添加文本</h2>");         
alert("do.1")
},function(){
alert("do.2")
//$("#text2").remove();         
});         
}); 

热点排行