js原生forEach、map与jquery的each、$.each的区别

 1 <!DOCTYPE html>
 2 <html >
 3 
 4 <head>
 5     <meta charset="UTF-8">
 6     <title>rem phone test</title>
 7     <meta name="viewport" content="user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
 8     <style>
 9         *{margin:0;padding:0}
10         body{
11             font-size:12px;
12         }
13         p{
14             font-size:14px;
15         }
16        .demo{width:rem;height:20rem;background:#00f}
17         ul li{widthL}
18     </style>
19 
20 </head>
21 
22 <body>
23 
24 <div class="demo">
25     <ul>
26         <li></li>
27         <li></li>
28         <li></li>
29         <li></li>
30         <li></li>
31     </ul>
32 </div>
33 <script src="http://libs.useso.com/js/jquery/1.8.3/jquery.min.js"></script>
34 <script>
35 var a = [1,1,1,1,1,1,1];
36 //forEach与map的参数顺序与jquery each $.each的顺序正好相反,js的顺序为,先element再index
37 a.forEach(function(element,index){
38     console.log(element);
39 });
40 var b = a.map(function(element,index){
41    return 2
42 });
43 console.log(b);
44 //jquery的顺序为先index再 element,并且,$.each除了传递 index 与element 还可以传递别的参数,
45 //index与element就会失效;
46 //注意使用 .each的时候,需要将数组转换为jquery数组;
47 $(function(){
48     $(a).each(function(index,element){
49         console.log(element)
50     });
51 
52     $.each(a,function(index,element){
53         console.log(element);
54 //        若需要对element进行jquery方法的操作,需要按照下面这种方式书写,将其转换为jquery对象;
55 //        console.log($(element))
56     });
57 
58 //    $.each传递其它参数用法;
59     $.each(a,function(e1,e2,e3){
60         console.log(e2);
61 
62     },[11,22,33])
63 });
64 
65 </script>
66 </body>
67 
68 </html>

相关文章:

http://www.cnblogs.com/mabelstyle/archive/2013/02/19/2917260.html