博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
jQuery中$().each与$.each的区别
阅读量:7028 次
发布时间:2019-06-28

本文共 2223 字,大约阅读时间需要 7 分钟。

在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.

  • function
    Type:  (   index,   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.

  • foo
  • bar
$( "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.

    • array
      Type: 
      The array to iterate over.
    • callback
      Type:  (   indexInArray,   value )
      The function that will be executed on every object.
    • object
      Type: 
      The object to iterate over.
    • callback
      Type:  (   propertyName,   valueOfProperty )
      The function that will be executed on every object.

The $.each() function is not the same as , 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 );});

 

转载于:https://www.cnblogs.com/peach/p/6652383.html

你可能感兴趣的文章
python基础练习-购物车
查看>>
poj 2406 && poj 1961 (KMP)
查看>>
Flutter: AnimatedCrossFade 在两个给定的子节点之间交叉淡化,避免大量三元表达式...
查看>>
iOS圆形图片裁剪,原型图片外面加一个圆环
查看>>
位运算
查看>>
Lucene:Query
查看>>
安卓文件的资源访问
查看>>
Java变量的作用域和生存期
查看>>
EXCEL IF 函数 模糊查询
查看>>
Angularjs 中的 controller
查看>>
ZigZag Conversion
查看>>
SQLLoader8(加载的数据中有换行符处理方法)
查看>>
表的创建、修改及约束
查看>>
Java
查看>>
nio简介
查看>>
用jquery如何获得服务器控件的aspnet的Id进行操作
查看>>
崩坏3mmd中的渲染技术研究
查看>>
Linux中断 - softirq
查看>>
《条目十八》避免使用vector<bool>
查看>>
Hadoop_14_MapReduce框架结构及其运行流程
查看>>