<!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

解决方案 »

  1.   

    remove方法:
    从DOM中删除所有匹配的元素。这个方法不会把匹配的元素从jQuery对象中删除,因而可以在将来再使用这些匹配的元素。但除了这个元素本身得以保留之外,其他的比如绑定的事件,附加的数据等都会被移除。事件还是在循环,只是元素上的事件被删除了
     
     $(function(){        
    $("#change").toggle(function(){  
    alert($('#text2'))
    //$("#text2").append("<h2>添加文本</h2>");         
    alert("do.1")
    },function(){
    alert("do.2")
    // $("#text2").remove();         
    });         
    }); 
      

  2.   

    用这个方法
    $("#text2").empty() 
    试试
      

  3.   


    谢了,明白了。用empty就可以清空。。