<head>
<script type="text/javascript" src="http://www.w3school.com.cn/jquery/jquery.js"></script>
<script type="text/javascript">
   $(function(){
    $("button").toggle(
    function(){
    alert("xxx");},
    function(){
    alert("bbb");}
  );
})
</script>
</head>
<body>
<button>请点击这里,来切换不同的背景颜色</button>
<p>xxxxxx</p>
</body>   
为什么载入页面后button没有消失,这里的引入的是w3c页面中<head>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript">
   $(function(){
    $("button").toggle(
    function(){
    alert("xxx");},
    function(){
    alert("bbb");}
  );
})
   
</script>
</head>
<body>
<button>请点击这里,来切换不同的背景颜色</button>
<p>xxxxxx</p>
</body>   
这里的button怎么已载入页面就消失了,这引入的jQuery是我在官网下的
求解惑,那个是对的。我的toggle()方法理解的是
$("button").toggle(function(){
   alert("xxx");
})点击button后弹出想“xxx”并且button消失
在线求解jquery

解决方案 »

  1.   


    <script src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript">
    /*toggle() 方法切换元素的可见状态。如果被选元素可见,则隐藏这些元素,如果被选元素隐藏,则显示这些元素。*/
    $(function(){
    $("#abb").hide();//首先隐藏按钮2
    $("#abv").toggle(function(){
    alert("按钮1将会被隐藏");//按钮1原本显示,调用该函数后隐藏
    })
    $("#abb").toggle(function(){//按钮2原本隐藏,调用该函数后显示
    $(this).css("width","200px");//为了让你区分按钮1和按钮2,我把按钮2大小调整了一下
    alert("按钮2将会被显示");
    })
    })
    </script>
    <input type="button"  id="abv" value="测试用按钮1"/>
    <input type="button"  id="abb" value="测试用按钮2"/>
      

  2.   

    <html>
    <head>
    <script type="text/javascript" src="http://www.w3school.com.cn/jquery/jquery.js"></script>
    <script type="text/javascript">
       $(function(){
        $("button").toggle(
        function(){
        alert("xxx");},
        function(){
        alert("bbb");}
      );
    })
       
    </script>
    </head>
    <body>
    <button>请点击这里,来切换不同的背景颜色</button>
    <p>xxxxxx</p>
    </body>
    </html>你把这个代码运行下,按理来说是载入页面后button会消失然后会弹出xxx的对话框
    但实际上button没消失。
      

  3.   

    jquery-1.9.1.js 与之前的版本有着很多差异
    不知道是 BUG,还是什么
      

  4.   

    toggle这个方法一直有BUG,但是jQuery官方为了性能,决定不去修复。楼主可以在网上查到相关信息。