zoukankan      html  css  js  c++  java
  • 前端学习笔记(zepto或jquery)——对li标签的相关操作(三)

    对li标签的相关操作——八种方式遍历li标签并获取其值

        $("ul>li").forEach(function(item,index){
                alert(index+":"+ item.innerHTML);
            });
            $("ul>li").each(function(index,item){
                alert(index+":"+ item.innerHTML);
            });
    $("ul>li").each(function(index){
                alert(index+":"+$(this).html());
            });
        $('ul>li').filter(function(index){
                 alert(index+":"+$(this).html());
            })
    $('ul>li').map(function(index,item){
                alert(index+":"+ item.innerHTML);
            });
    $("ul>li").map(function(index){
                alert(index+":"+$(this).html());
            });
    $.grep($("ul>li").get(), function(item,index){ 
                alert(index + ":" + item.innerHTML);
            }); 
            $.grep($("ul>li").get(), function(item){ 
                alert(item.innerHTML);
            }); 

    注:filter没有两个参数的,forEach没有一个参数的

      其中一个重要的区别是,each返回的是原来的数组,并不会新创建一个数组。而map方法会返回一个新的数组。

          如果在没有必要的情况下使用map,则有可能造成内存浪费。

          使用each时,改变的还是原来的items数组,而使用map时,不改变items,只是新建一个新的数组。

  • 相关阅读:
    组合算法实现
    Memcached 和 Redis 分布式锁方案
    CLR 内存分配和垃圾收集 GC
    Windbg 的使用和常用命令
    Geohash 算法学习
    经纬度计算
    Windbg 分析CPU上涨
    Windbg 分析内存上涨
    django基于存储在前端的token用户认证
    非常详细的Django使用Token(转)
  • 原文地址:https://www.cnblogs.com/xuhang/p/4228161.html
Copyright © 2011-2022 走看看