jquery 的 for 迴圈

來源:互聯網
上載者:User

標籤:寫法   鏈式   table   on()   checkbox   tab   迴圈   each   函數實現   

jquery 的 for 迴圈:

1、 var userList = [11,22,33,44];

  $.each(userList,function(i,item){

    console.log(i, item);

  });

  結果輸出:

      0 11

    1 22

  2 33

  3 44

用法:  $.each()  第一個參數是迴圈的對象 , 第二個參數對對象中的每一個元素 執行 function函數 ,function 的第一個參數 i 是索引,item 是 迴圈對象中的每一個元素。

 

一般的寫法:  for(var i in userList){}   這裡面的 i 是 索引。

如果迴圈對象是 字典,上面的 i, item 分別是 key , value 。

 

2、 如果有一個 table 包含多個 tr , 每個 tr 包含多個 td , td 中 有 checkbox 。如果要迴圈 checkbox 。可以用以下的寫法:

function ReverseAll(){

     $(‘table :checkbox‘).each(function(){

       var isChecked = $(this).prop(‘checked‘);     // $(this) 擷取當前的迴圈對象,此例中表示 table 中的 checkbox 子項目 集合中的一個元素

       if(isChecked) { $(this).prop("checked",false); }

       else{ $(this).prop("checked",true);  }

  }) ;

}                                                      //此函數實現對 table表中所有的checkbox 反選功能。

  此處用 $(‘table :checkbox‘) 選取器 擷取的 table 對象中的所有 checkbox 子項目,並且對每個子項目 執行 function 。是一種鏈式編程。

jquery 的 for 迴圈

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.