zoukankan      html  css  js  c++  java
  • $.each()和$().each(),以及forEach()的用法

    1.forEach是js中遍历数组的方法,如下

    var arr=[1,2,3,4];
    arr.forEach(function(val,index,arr){//val为数组中当前的值,index为当前值的下表,arr为原数组
    arr[index]=2*val;
    });
    console.log(arr);//结果是修改了原数组,为每个数乘以2

    2.$.each()是jquery中遍历数组的方法,如下

    var arr=[1,2,3,4];
    $.each(arr,function(i,n){
    alert("索引"+i+"对应的值"+n);
    });

    3.$().each()方法规定为每个匹配元素规定运行的函数,如下:

    <!DOCTYPE html>
    <html>
    <head>
    <script type="text/javascript" src="js/jquery.min.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>
    如果有什么不对的地方欢迎指正!

  • 相关阅读:
    省常中模拟 Test4
    省常中模拟 Test3 Day1
    省常中模拟 Test3 Day2
    省常中模拟 Test1 Day1
    树型动态规划练习总结
    noip2010提高组题解
    noip2003提高组题解
    noip2009提高组题解
    noip2004提高组题解
    noip2002提高组题解
  • 原文地址:https://www.cnblogs.com/longailong/p/6409172.html
Copyright © 2011-2022 走看看