zoukankan      html  css  js  c++  java
  • jQuery4——jQuery中的map静态方法,jQuey中的其他静态方法

    map静态方法


    <script>
        var arr = [1, 3, 5, 7, 9];
        var obj = {0: 1, 1: 3, 2: 5, 3: 7, 4: 9, length: 5};
        //利用原生js的map方法遍历
        /*
        第一个参数:当前遍历的元素
        第二个参数:当前遍历的索引
        第三个参数:当前遍历的数组
         */
        arr.map(function (value, index, array) {
            console.log(value, index, array);
        });
        //注意点:不能遍历伪数组
        /*obj.map(function (value, index, array) {
            console.log(value,index,array);
        });*/
    
        /*
        jQuery中的map方法
        第一个参数:数组
        第二个参数:回调函数
        回调函数中的参数:第一个:要遍历的元素
        第二个:遍历到的索引
         */
        /*$.map(arr, function (value, index) {
            console.log(index, value);
        });*/
    
        /*
        注意点:可以遍历伪数组
         */
        var res1 = $.map(obj, function (value, index) {
            console.log(index, value);
            return value+index;
        });
    
        var res2 = $.each(obj, function (index, value) {
            console.log(index, value);
        });
    
        /*
        jQuery中each和map方法的区别:
        each静态方法的返回值是遍历谁就返回谁,
        map静态方法的返回值是一个空数组
    
        each方法不支持在回调函数中对遍历的数组进行处理;
        each方法可以在回调函数中通过return对遍历的数组进行处理,
        然后生成新的数组返回
    
         */
        console.log(res1);
        console.log(res2);
    </script>
    

    jQuey中的其他静态方法


    /*
        $.trim():
        作用:去除字符串两端的空格
        参数:需要去除空格的字符串
        返回值:去除空格后的字符串
         */
        /*var s = '  ghj ';
        var trim = $.trim(s);
        console.log('--' + trim + '--');*/
    
        /*
         $.isWindow();
         作用:判断传入的对象是否是window对象
         返回值:true/false
         */
        //真数组
        var arr = [1, 3, 5, 7, 9];
        //伪数组
        var obj = {0: 1, 1: 3, 2: 5, 3: 7, 4: 9, length: 5};
        //函数
        var fn = function () {
        };
        //window对象
        var w = window;
    
        var res = $.isWindow(w);
        console.log(res);
    
        /*
        $.isArray();
        作用:判断传入的对象是否是array数组
        返回值:true/false
        */
        var array = $.isArray(arr);
        console.log(array);
    
        /*
        $.isFunction();
        作用:判断传入的对象是否是一个函数
        返回值:true/false
        */
        var function1 = $.isFunction(fn);
        console.log(function1);
    
        /*
        注意点:jQuery框架本质上是一个函数
         */
        var function2 = $.isFunction(jQuery);
        console.log(function2);
    </script>
    

      

  • 相关阅读:
    Gin 框架的使用
    Beego model 增删改查
    Beego 搭建
    Go 如何快速解决依赖管理问题
    Go 语法基础
    使用vue控制元素显示隐藏
    Vue 刷新页面方式 和页面跳转的方式
    如何让nginx 显示文件夹目录
    Vue(项目配置iview的upload插件上传文件 )+ Python flask 上传文件
    uwsgi 常见配置
  • 原文地址:https://www.cnblogs.com/ghlz/p/13306506.html
Copyright © 2011-2022 走看看