如何使用jQuery的each方法遍历对象属性?
- 内容介绍
- 文章标签
- 相关推荐
本文共计249个文字,预计阅读时间需要1分钟。
注意:jQuery 中无论是 `$($).each(function(index, value){})` 还是 `$$.each(arr, function(index, value){})`,第一个参数都是 `index`,代表当前元素的索引。
注意1.jquery中不管是$().each(function(index,value){})还是$.each(arr,function(index,value){注意: 1.jquery中不管是 $().each(function(index,value){})还是$.each(arr,function(index,value){})自定义函数的,第一个参数都是index , 第二个参数 是value.
2.原生js中的forEach(function(value,index,arr){}) 自定义函数的,第一个参数都是value, 第二个参数 是index .
1.forEach是js中遍历数组的方法
var arr=[1,2,3,4];arr.forEach(function(val,index,arr){//val为数组中当前的值,index为当前值的下表,arr为原数组arr[index]=2*val;});console.log(arr);//结果是修改了原数组,为每个数乘以2
2.$.each()是jquery中遍历数组的方法
var arr=[1,2,3,4];$.each(arr,function(i,n){alert("索引"+i+"对应的值"+n);});
3.$().each()方法规定为每个匹配元素规定运行的函数
输出每个列表项的值
- Coffee
- Milk
- Soda
本文共计249个文字,预计阅读时间需要1分钟。
注意:jQuery 中无论是 `$($).each(function(index, value){})` 还是 `$$.each(arr, function(index, value){})`,第一个参数都是 `index`,代表当前元素的索引。
注意1.jquery中不管是$().each(function(index,value){})还是$.each(arr,function(index,value){注意: 1.jquery中不管是 $().each(function(index,value){})还是$.each(arr,function(index,value){})自定义函数的,第一个参数都是index , 第二个参数 是value.
2.原生js中的forEach(function(value,index,arr){}) 自定义函数的,第一个参数都是value, 第二个参数 是index .
1.forEach是js中遍历数组的方法
var arr=[1,2,3,4];arr.forEach(function(val,index,arr){//val为数组中当前的值,index为当前值的下表,arr为原数组arr[index]=2*val;});console.log(arr);//结果是修改了原数组,为每个数乘以2
2.$.each()是jquery中遍历数组的方法
var arr=[1,2,3,4];$.each(arr,function(i,n){alert("索引"+i+"对应的值"+n);});
3.$().each()方法规定为每个匹配元素规定运行的函数
输出每个列表项的值
- Coffee
- Milk
- Soda

