Difference: When traversing the DOM, the $(selector).each() function is usually used, which is often used in DOM processing; when traversing the data, the $.each() function is usually used. This function is commonly used in data processing.
[Related recommendations:jQuery video tutorial]
In jquery, traversing objects and arrays is often used To $().each and $.each(), two methods.
$().each is often usedin DOM processing. If the page has multiple input tags of checkbox type, use $().each to process multiple checkbooks, for example:
{ if($(this).attr(‘checked’)==true) { //一些操作代码 } 回调函数是可以传递参数,i就为遍历的索引。
Traversing an array is usually done with $.each(), for example:
$.each([{name:"limeng",email:"xfjylimeng"},{name:"hehe",email:"xfjylimeng"}],function(i,n) { alert("索引:"+i+"对应值为:"+n.name); });
The parameter i is the traversal index value, n is the current traversal object.
var arr1 = [ "one", "two", "three", "four", "five" ]; $.each(arr1, function(){ alert(this); });
Output: one two three four five
var arr2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] $.each(arr2, function(i, item){ alert(item[0]); });
Output: 1 4 7
var obj = { one:1, two:2, three:3, four:4, five:5 }; $.each(obj, function(key, val) { alert(obj[key]); });
Output: 1 2 3 4 5
For more programming-related knowledge, please visit:Programming Video! !
The above is the detailed content of What is the difference between $().each and $.each in jquery?. For more information, please follow other related articles on the PHP Chinese website!