jquery each traversal

Source: Internet
Author: User

In jquery, iterating over objects and arrays often uses $ (). Each and $.each (), two methods.

$ (). Each uses 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 action code } The callback function is a parameter that can be passed, and I is the index of the traversal. 

Traversing an array is usually handled with $.each () for example:

$.each ([{name: "Limeng", Email: "Xfjylimeng"},{name: "hehe", Email: "Xfjylimeng"}],function(i,n) {alert ("Index:" +i+ "corresponds to the value:" +n.name);}); Parameter i is the traversal index value, and n is the current traversal object.varARR1 = ["One", "one", "one", "three", "four", "five"];$.each (arr1,function() {alert ( This);}); Output: One, three four fivevarARR2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]$.each (ARR2,function(I, item) {Alert (item[0]);}); Output:1 4 7varobj = {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 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 (b) {alert (a+b);} function Sub (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.

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

JQuery each source

eachfunction(obj, callback) {varLength, i = 0; if(Isarraylike (obj)) {length=obj.length;  for(; i < length; i++ ) {                if(Callback.call (obj[i], I, obj[i]) = = =false ) {                     Break; }            }        } Else {             for(Iinchobj) {                if(Callback.call (obj[i], I, obj[i]) = = =false ) {                     Break; }            }        }        returnobj; }

jquery each traversal

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.