zoukankan      html  css  js  c++  java
  • js原生forEach、map与jquery的each、$.each的区别

     1 <!DOCTYPE html>
     2 <html lang="zh">
     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{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

    坚持下去就能成功
  • 相关阅读:
    C++的精髓——代码复用、接口复用
    静态库和动态库的区别和win平台和linux平台代码实现
    windows工程总结
    预编译头文件stdafx.h-stdafx.cpp-stdafx.pch(pre-compile headfile)
    linux调用库的方式
    Window 32位 编程总结
    读Zepto源码之内部方法
    读Zepto源码之代码结构
    再谈 javascript 数组去重
    把axios封装为vue插件使用
  • 原文地址:https://www.cnblogs.com/suoking/p/5050343.html
Copyright © 2011-2022 走看看