zoukankan      html  css  js  c++  java
  • jQuery中$().each与$.each的区别

    在jQuery中 $().each与$.each是不同的,$().each用于对jQuery对象做遍历操作处理,而$.each用于循环遍历一个Array或Object对象,相当于for或while循环写法。

    .each( function )

    Description: Iterate over a jQuery object, executing a function for each matched element.

    .each( function )

    • function
      Type: FunctionInteger index, Element element )
      A function to execute for each matched element.

    The .each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element.

    <ul>
      <li>foo</li>
      <li>bar</li>
    </ul>
    $( "li" ).each(function( index ) {
      console.log( index + ": " + $( this ).text() );
    });

    jQuery.each( array, callback )

    Description: A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.

    The $.each() function is not the same as $(selector).each(), which is used to iterate, exclusively, over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through the this keyword, but Javascript will always wrap the this value as an Object even if it is a simple string or number value.) The method returns its first argument, the object that was iterated.

    $.each([ 52, 97 ], function( index, value ) {
      alert( index + ": " + value );
    });
    
    var obj = {
      "flammable": "inflammable",
      "duh": "no duh"
    };
    $.each( obj, function( key, value ) {
      alert( key + ": " + value );
    });
  • 相关阅读:
    .net大文件上传
    java文件上传和下载
    文件上传系统
    plupload上传大文件
    代码坏味道之夸夸其谈的未来性
    freemarker中的substring取子串
    【翻译自mos文章】在Oracle GoldenGate中循环使用ggserr.log的方法
    3种浏览器性能測试
    SDUTOJ 2772 KMP简单应用
    C++库研究笔记--用__attribute__((deprecated)) 管理过时代码
  • 原文地址:https://www.cnblogs.com/peach/p/6652383.html
Copyright © 2011-2022 走看看