现在的位置: 首页 > 综合 > 正文

jQuery方法Toggle()应用

2013年01月28日 ⁄ 综合 ⁄ 共 1265字 ⁄ 字号 评论关闭
 

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml" >
  3. <head>
  4.     <title>toggle</title>
  5.     <script type="text/javascript" src="../js/jquery-1.2.6.js"></script>
  6.     <script type="text/javascript">
  7.         //传f(id),此为正道,这里是传了object
  8.         function f(obj){
  9.             var id = $(obj).attr("id");
  10.             $(".cc:not(#"+id+")").hide();//传过来的是id
  11.             $(obj).toggle();//直接传值就是当id,#id
  12.         }
  13.         //传'id',此法不简化
  14.         function ff(obj){
  15.             $(".cc:not(#"+obj+")").hide();//仍然需要这样
  16.             $("#"+obj).toggle();
  17.         }
  18.     </script>
  19. </head>
  20. <body>
  21. <div style="display:none;" id="Div1" class="cc">hello1</div>
  22.     <input type="button" value="1" onclick="f(Div1);" />
  23. <div style="display:none;" id="Div2" class="cc">hello2</div>
  24.     <input type="button" value="2" onclick="f(Div2);" />
  25. <div style="display:none;" id="Div3" class="cc">hello3</div>
  26.     <input type="button" value="3" onclick="f(Div3);" />
  27. <div style="display:none;" id="Div4" class="cc">hello4</div>
  28.     <input type="button" value="4" onclick="ff('Div4');" />
  29. <div style="display:none;" id="Div5" class="cc">hello5</div>
  30.     <input type="button" value="5" onclick="ff('Div5');" />
  31. </body>
  32. </html>

抱歉!评论已关闭.