Home > Web Front-end > JS Tutorial > body text

What is the difference between $().each and $.each in jquery?

青灯夜游
Release: 2020-11-30 13:59:13
Original
2004 people have browsed it

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.

What is the difference between $().each and $.each in jquery?

[Related recommendations: jQuery video tutorial]

In jquery, traversing objects and arrays is often used To $().each and $.each(), two methods.

$().each is often used in 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就为遍历的索引。
Copy after login

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);
});
Copy after login

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);
});
Copy after login

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]);
});
Copy after login

Output: 1 4 7

var obj = { one:1, two:2, three:3, four:4, five:5 };
$.each(obj, function(key, val) {
alert(obj[key]);
});
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template