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,只是新建一个新的数组。

  • 相关阅读:
    Linux的学习--系统目录
    PHP内核的学习--创建PHP扩展
    PHP的学习--连接MySQL的三种方式
    MIME Type
    颜色的命名
    JavaScript的学习--生成二维码
    MySQL的学习--触发器
    Google Guava之--cache
    java代码调用oracle存储过程
    oracle序列
  • 原文地址:https://www.cnblogs.com/xuhang/p/4228161.html
Copyright © 2011-2022 走看看