[ javasript ] javascript中的each遍历!

1.数组中的each

 

[javascript] view plain copy
 
  1.  var arr = [ "one", "two", "three", "four"];       
  2.  $.each(arr, function(){       
  3.     alert(this);       
  4.  });     
  5. //上面这个each输出的结果分别为:one,two,three,four      
  6.       
  7. var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]]       
  8. $.each(arr1, function(i, item){       
  9.    alert(item[0]);       
  10. });       
  11. //其实arr1为一个二维数组,item相当于取每一个一维数组,     
  12. //item[0]相对于取每一个一维数组里的第一个值     
  13. //所以上面这个each输出分别为:1   4   7       
  14.     
  15.     
  16. var obj = { one:1, two:2, three:3, four:4};       
  17. $.each(obj, function(i) {       
  18.     alert(obj[i]);             
  19. });     
  20. //这个each就有更厉害了,能循环每一个属性       
  21. //输出结果为:1   2  3  4  


2.遍历Dom元素中

 

 

[javascript] view plain copy
 
  1. <html>  
  2. <head>  
  3. <script type="text/javascript" src="/jquery/jquery.js"></script>  
  4. <script type="text/javascript">  
  5. $(document).ready(function(){  
  6.   $("button").click(function(){  
  7.     $("li").each(function(){  
  8.       alert($(this).text())  
  9.     });  
  10.   });  
  11. });  
  12. </script>  
  13. </head>  
  14. <body>  
  15. <button>输出每个列表项的值</button>  
  16. <ul>  
  17. <li>Coffee</li>  
  18. <li>Milk</li>  
  19. <li>Soda</li>  
  20. </ul>  
  21. </body>  
  22. </html>  


依次弹出Coffee,Milk,Soda

 

3.each和map的比较

 

[javascript] view plain copy
 
  1. $(function(){  
  2.     var arr = [];  
  3.     $(":checkbox").each(function(index){  
  4.         arr.push(this.id);  
  5.     });  
  6.     var str = arr.join(",");  
  7.     alert(str);  
  8. })  

 

 

map方法:

将每个:checkbox执行return this.id;并将这些返回值,自动的保存为jQuery对象,然后用get方法将其转换成原生JavaScript数组,再使用join方法转换成字符串,最后alert这个值;

[javascript] view plain copy
 
  1. $(function(){  
  2.     var str = $(":checkbox").map(function() {  
  3.         return this.id;  
  4.     }).get().join();      
  5.     alert(str);  
  6. })  


当有需一个数组的值的时候,用map方法,很方便。

 

4.jquery中使用each

例遍数组,同时使用元素索引和内容。(i是索引,n是内容)

代码如下:

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

 

例遍对象,同时使用成员名称和变量内容。(i是成员名称,n是变量内容)
代码如下:
[javascript] view plain copy
 
  1. $.each( { name: "John", lang: "JS" }, function(i, n){  
  2. alert( "Name: " + i + ", Value: " + n );  
  3. });   

例遍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如下

代码如下:
[javascript] view plain copy
 
  1. $.each($("input:hidden"), function(i,val){  
  2. alert(val); //输出[object HTMLInputElement],因为它是一个表单元素。  
  3. alert(i); //输出索引为0,1,2,3  
  4. alert(val.name); //输出name的值  
  5. alert(val.value); //输出value的值  
  6. });  


5.each中根据this查找元素
实现效果”回复”两个字只有在鼠标经过的时候才显示出来

 

[html] view plain copy
 
  1. <ol class="commentlist">  
  2.     <li class="comment">  
  3.         <div class="comment-body">  
  4.           <p>嗨,第一层评论</p>  
  5.           <div class="reply">  
  6.             <href="#" class=".comment-reply-link">回复</a>  
  7.           </div>  
  8.         </div>  
  9.         <ul class="children">  
  10.           <li class="comment">  
  11.             <div class="comment-body">  
  12.             <p>第二层评论</p>  
  13.             <div class="reply">  
  14.               <href="#" class=".comment-reply-link">回复</a>  
  15.             </div>  
  16.           </div></li>  
  17.         </ul>  
  18.     </li>  
  19. </ol>  


js代码如下

 

 

[javascript] view plain copy
 
  1. $("div.reply").hover(function(){  
  2.   $(this).find(".comment-reply-link").show();  
  3. },function(){  
  4.   $(this).find(".comment-reply-link").hide();  
  5. });  


实现效果,验证判断题是否都有选择

 

 

[html] view plain copy
 
  1. <ul id="ulSingle">  
  2.       
  3.             <li class="liStyle">  
  4.                 1.  阿斯顿按时<label id="selectTips" style="display: none" class="fillTims">请选择</label>  
  5.                 <!--begin选项-->  
  6.                 <ul>  
  7.                       
  8.                             <li class="liStyle2">  
  9.                                 <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" />  
  10.                                 <input id="repSingle_repSingleChoices_0_cheSingleChoice_0" type="checkbox" name="repSingle$ctl00$repSingleChoices$ctl00$cheSingleChoice" /></li>  
  11.                           
  12.                             <li class="liStyle2">  
  13.                                 <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" />  
  14.                                 <input id="repSingle_repSingleChoices_0_cheSingleChoice_1" type="checkbox" name="repSingle$ctl00$repSingleChoices$ctl01$cheSingleChoice" /></li>  
  15.                           
  16.                             <li class="liStyle2">  
  17.                                 <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" />  
  18.                                 <input id="repSingle_repSingleChoices_0_cheSingleChoice_2" type="checkbox" name="repSingle$ctl00$repSingleChoices$ctl02$cheSingleChoice" /></li>  
  19.                           
  20.                 </ul>  
  21.                 <!--end选项-->  
  22.                 <br />  
  23.             </li>  
  24.           
  25. </ul>  

 

[javascript] view plain copy
 
  1. //验证单选题是否选中  
  2.         $("ul#ulSingle>li.liStyle").each(function (index) {  
  3.             //选项个数  
  4.             var count = $(this).find("ul>li>:checkbox").length;  
  5.             var selectedCount = 0  
  6.             for (var i = 0; i < count; i++) {  
  7.                 if ($(this).find("ul>li>:checkbox:eq(" + i + ")").attr("checked")) {  
  8.                     selectedCount++;  
  9.                     break;  
  10.                 }  
  11.             }  
  12.             if (selectedCount == 0) {  
  13.                 $(this).find("label#selectTips").show();  
  14.                 return false;  
  15.             }  
  16.             else {  
  17.                 $(this).find("label#selectTips").hide();  
  18.             }  
  19.         })  


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

jQuery.each(object, [callback]) 

概述 
通用例遍方法,可用于例遍对象和数组。 

不同于例遍 jQuery 对象的 $().each() 方法,此方法可用于例遍任何对象。回调函数拥有两个参数:第一个为对象的成员或数组的索引,第二个为对应变量或内容。如果需要退出 each 循环可使回调函数返回 false,其它返回值将被忽略。 

参数 
objectObject 
需要例遍的对象或数组。 

callback (可选)Function 
每个成员/元素执行的回调函数。

 

原文出处;http://www.cnblogs.com/tylerdonet/archive/2013/04/05/3000618.html

posted @ 2017-05-10 11:04  553490191  阅读(460)  评论(0编辑  收藏  举报