jquery's $ (). The difference between Each,$.each

Source: Internet
Author: User

In jquery, iterating over objects and arrays often uses $ (). Each and $.each (), two methods. The two methods are differentiated, and the two methods show their respective characteristics for different operations.

$ (). Each, for this method, use more of the DOM processing. If the page has more than one input label type of checkbox, for this time use $ (). Each to process multiple checkbook, for example:

$ ("input[name= ' ch ']"). each (function (i) {
if ($ (this). attr (' checked ') ==true)
{
Some operation codes

}

A callback function is a parameter that can be passed, and I is the index of the traversal.

For traversing an array, using $.each () to deal with, it is absolutely cool to the extreme. For example:

$.each ([{"Name": "Limeng", "email": "Xfjylimeng"},{"name": "hehe", "email": "Xfjylimeng"},function (i,n)
{
Alert ("Index:" +i, "the corresponding value is:" +n.name);
});

Parameter i is the traversal index value, and n is the current traversal object.

var arr1 = ["One", "one", "one", "three", "four", "five");
$.each (arr1, function () {
alert (this);
});
Output: One, 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

In jquery there is a each method, use it very cool, no longer like the original for The loop, jquery source itself has a lot to use each method.

In fact, the each method in jquery is implemented by the call method in JS.

Here's a quick introduction to the call method.
Call this method is wonderful, in fact, the official note is: "Invoke a method of an object, replace the current object with another object." "The explanation on the web is to transform the context, or to change the context of this pointer," he said.
Call ([thisobj[,arg1[, arg2[, [,. ArgN]]])

Parameters
Thisobj
Options are available. The object that will be used as the current object.
Arg1, Arg2,, ArgN
Options are available. The method parameter sequence will be passed.

Description
The call method can be used to invoke a method in place of another object. The call method can change the object context of a function from the initial context to a new object specified by Thisobj.

There is a classic example of a quote online

JS Code
function Add (A, B)
{
alert (A+B);
}
function Sub (A, b)
{
Alert (A-B);
}
Add.call (sub,3,1);

Replace Sub,add.call (sub,3,1) = = Add (3,1) with add, so the result is: alert (4);
Note: The function in JS is actually an object, and the function name is a reference to a function object.

Specific call more in-depth is not mentioned here.

Here are some common uses of each of the jquery methods

JS Code
var arr = ["One", "one", "three", "four"];
$.each (arr, function () {
alert (this);
});
The results for each of the above outputs are: One,two,three,four

var arr1 = [[1, 4, 3], [4, 6, 6], [7, 20, 9]
$.each (arr1, function (I, item) {
Alert (item[0]);
});
In fact, ARR1 is a two-dimensional array, item is equivalent to taking each one-dimensional array,
Item[0] Relative to the first value in each one-dimensional array
So the above each output is: 1 4 7

var obj = {one:1, two:2, Three:3, four:4};
$.each (obj, function (key, Val) {
Alert (Obj[key]);
});
This each one is more powerful, can loop each property
The output is: 1 2 3 4

    • This article fixed link: http://www.frontopen.com/1394.html
    • Reprint Please specify: Taste life September 29, 2013 in front-end pioneer published

jquery's $ (). The difference between Each,$.each

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.