jQuery方法Toggle()應用

來源:互聯網
上載者:User
 

  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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.