JQuery. each () usage sharing _ jquery

Source: Internet
Author: User
I wrote a small plug-in yesterday and used this method. I don't quite understand the $. each () method. I checked the manual today and learned how to use arrays and element indexes and content. (I is the index, n is the content)

The Code is as follows:


$. Each ([0, 1, 2], function (I, n ){
Alert ("Item #" + I + ":" + n );
});


Sample object and use both the member name and variable content. (I is the member name, and n is the variable content)

The Code is as follows:


$. Each ({name: "John", lang: "JS"}, function (I, n ){
Alert ("Name:" + I + ", Value:" + n );
});


This example uses an input form element as an example.

If you have such code in the dom




Then you use the each as follows:

The Code is as follows:


$. Each ($ ("input: hidden"), function (I, val ){
Alert (val); // output [object HTMLInputElement] because it is a form element.
Alert (I); // The output index is 0, 1, 2, 3.
Alert (val. name); // output the value of name
Alert (val. value); // output value
});


The following is an official explanation:

JQuery. each (object, [callback])

Overview
The general example method can be used for example object and array.

Unlike the $ (). each () method of the jQuery object, this method can be used to sample any object. The callback function has two parameters: the first is the object's member or array index, and the second is the corresponding variable or content. If you want to exit the each loop, the callback function returns false. Other return values are ignored.

Parameters
ObjectObject
Objects or arrays that need to be repeated.

Callback (optional) Function
The callback function executed by each member/element.

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.