javascript中的each遍曆!

來源:互聯網
上載者:User

1.數組中的each

 var arr = [ "one", "two", "three", "four"];      $.each(arr, function(){         alert(this);      });   //上面這個each輸出的結果分別為:one,two,three,four        var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]]     $.each(arr1, function(i, item){        alert(item[0]);     });     //其實arr1為一個二維數組,item相當於取每一個一維數組,   //item[0]相對於取每一個一維數組裡的第一個值   //所以上面這個each輸出分別為:1   4   7         var obj = { one:1, two:2, three:3, four:4};     $.each(obj, function(i) {         alert(obj[i]);           });   //這個each就有更厲害了,能迴圈每一個屬性     //輸出結果為:1   2  3  4

2.遍曆Dom元素中

<html><head><script type="text/javascript" src="/jquery/jquery.js"></script><script type="text/javascript">$(document).ready(function(){  $("button").click(function(){    $("li").each(function(){      alert($(this).text())    });  });});</script></head><body><button>輸出每個清單項目的值</button><ul><li>Coffee</li><li>Milk</li><li>Soda</li></ul></body></html>

依次彈出Coffee,Milk,Soda

3.each和map的比較

$(function(){    var arr = [];    $(":checkbox").each(function(index){        arr.push(this.id);    });    var str = arr.join(",");    alert(str);})

map方法:

將每個:checkbox執行return this.id;並將這些傳回值,自動的儲存為jQuery對象,然後用get方法將其轉換成原生Javascript數組,再使用join方法轉換成字串,最後alert這個值;

$(function(){    var str = $(":checkbox").map(function() {        return this.id;    }).get().join();        alert(str);})

當有需一個數組的值的時候,用map方法,很方便。

4.jquery中使用each

例遍數組,同時使用元素索引和內容。(i是索引,n是內容)

代碼如下:

$.each( [0,1,2], function(i, n){alert( "Item #" + i + ": " + n );}); 

例遍對象,同時使用成員名稱和變數內容。(i是成員名稱,n是變數內容)代碼如下:

$.each( { name: "John", lang: "JS" }, function(i, n){alert( "Name: " + i + ", Value: " + n );}); 

例遍dom元素,此處以一個input表單元素作為例子。 

如果你dom中有一段這樣的代碼 

<input name="aaa" type="hidden" value="111" /> 

<input name="bbb" type="hidden" value="222" /> 

<input name="ccc" type="hidden" value="333" /> 

<input name="ddd" type="hidden" value="444"/> 

然後你使用each如下

代碼如下:
$.each($("input:hidden"), function(i,val){alert(val); //輸出[object HTMLInputElement],因為它是一個表單元素。alert(i); //輸出索引為0,1,2,3alert(val.name); //輸出name的值alert(val.value); //輸出value的值});

5.each中根據this尋找元素
實現效果”回複”兩個字只有在滑鼠經過的時候才顯示出來

<ol class="commentlist">    <li class="comment">        <div class="comment-body">          <p>嗨,第一層評論</p>          <div class="reply">            <a href="#" class=".comment-reply-link">回複</a>          </div>        </div>        <ul class="children">          <li class="comment">            <div class="comment-body">            <p>第二層評論</p>            <div class="reply">              <a href="#" class=".comment-reply-link">回複</a>            </div>          </div></li>        </ul>    </li></ol>

js代碼如下

$("div.reply").hover(function(){  $(this).find(".comment-reply-link").show();},function(){  $(this).find(".comment-reply-link").hide();});

實現效果,驗證判斷題是否都有選擇

<ul id="ulSingle">                <li class="liStyle">                1.  阿斯頓按時<label id="selectTips" style="display: none" class="fillTims">請選擇</label>                <!--begin選項-->                <ul>                                                <li class="liStyle2">                                <span id="repSingle_repSingleChoices_0_labOption_0">A         </span>.阿薩德發<input type="hidden" name="repSingle$ctl00$repSingleChoices$ctl00$hidID" id="repSingle_repSingleChoices_0_hidID_0" value="1" />                                <input id="repSingle_repSingleChoices_0_cheSingleChoice_0" type="checkbox" name="repSingle$ctl00$repSingleChoices$ctl00$cheSingleChoice" /></li>                                                    <li class="liStyle2">                                <span id="repSingle_repSingleChoices_0_labOption_1">B         </span>.阿薩德發<input type="hidden" name="repSingle$ctl00$repSingleChoices$ctl01$hidID" id="repSingle_repSingleChoices_0_hidID_1" value="2" />                                <input id="repSingle_repSingleChoices_0_cheSingleChoice_1" type="checkbox" name="repSingle$ctl00$repSingleChoices$ctl01$cheSingleChoice" /></li>                                                    <li class="liStyle2">                                <span id="repSingle_repSingleChoices_0_labOption_2">C         </span>.阿斯頓<input type="hidden" name="repSingle$ctl00$repSingleChoices$ctl02$hidID" id="repSingle_repSingleChoices_0_hidID_2" value="3" />                                <input id="repSingle_repSingleChoices_0_cheSingleChoice_2" type="checkbox" name="repSingle$ctl00$repSingleChoices$ctl02$cheSingleChoice" /></li>                                        </ul>                <!--end選項-->                <br />            </li>        </ul>

//驗證單選題是否選中        $("ul#ulSingle>li.liStyle").each(function (index) {            //選項個數            var count = $(this).find("ul>li>:checkbox").length;            var selectedCount = 0            for (var i = 0; i < count; i++) {                if ($(this).find("ul>li>:checkbox:eq(" + i + ")").attr("checked")) {                    selectedCount++;                    break;                }            }            if (selectedCount == 0) {                $(this).find("label#selectTips").show();                return false;            }            else {                $(this).find("label#selectTips").hide();            }        })

6.官方解釋
以下是官方的解釋: 

jQuery.each(object, [callback]) 

概述 
通用例遍方法,可用於例遍對象和數組。 

不同於例遍 jQuery 對象的 $().each() 方法,此方法可用於例遍任何對象。回呼函數擁有兩個參數:第一個為對象的成員或數組的索引,第二個為對應變數或內容。如果需要退出 each 迴圈可使回呼函數返回 false,其它傳回值將被忽略。 

參數 
objectObject 
需要例遍的對象或數組。 

callback (可選)Function 
每個成員/元素執行的回呼函數。

原文出處;http://www.cnblogs.com/tylerdonet/archive/2013/04/05/3000618.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.