$. Each and $ (selector). Each ()

Source: Internet
Author: User
Translated from the official manual: jquery. Each () processes arrays or object content cyclically Jquery. Each (collection, callback (indexinarray, valueofelement ))

CollectionThe object or array to be traversed.

Callback (indexinarray, valueofelement)Functions called on each object

Description

A common traversal function that can be used to traverse objects and arrays. arrays and pseudo array objects containing a Length attribute (pseudo array objects such as function arguments objects) are traversed by numerical indexes, from 0 to length-1, other objects are traversed through attributes.

$. Each () and $ (selector ). different from each (), the latter is used to traverse jquery objects. The former can be used to traverse any set (whether an array or an object). If it is an array, each time the callback function imports an array index and the corresponding value (the value can also be obtained through the this keyword, but JavaScript will always wrap this value as an object-although it is a string or a number ), the method returns the first parameter of the retrieved object.

++ ++

// Example: --- input an array

<! Doctype HTML> // Output

0: 52 1: 97

++ ++

// Example: --- if a ing is used as a set, the callback function imports a key-value pair each time.

<! Doctype HTML> // Output

Flammable: inflammable Duh: No duh

++ ++

// Example: --- you can exit $. Each () when return false in the callback function. If a non-false value is returned, it will immediately go to the next traversal like using continue in the for loop.

<! Doctype HTML>

<HTML>

<Head>

<Style>

Div {color: Blue ;}

Div # Five {color: red ;}

</Style>

<SCRIPT src = "http://code.jquery.com/jquery-latest.js”> </SCRIPT>

</Head>

<Body>

<Div id = "one"> </div>

<Div id = "two"> </div>

<Div id = "three"> </div>

<Div id = "four"> </div>

<Div id = "five"> </div>

<SCRIPT>

VaR arr = ["one", "two", "three", "four", "five"]; // Array

VaR OBJ = {one: 1, two: 2, three: 3, four: 4, five: 5}; // object

Jquery. Each (ARR, function () {// This specifies the value

$ ("#" + This). text ("Mine is" + This + "."); // This points to an array value, such as one or two

Return (this! = "Three"); // exit traversal if this = three

});

Jquery. Each (OBJ, function (I, Val) {// I points to the key, Val specifies the value

$ ("#" + I). append (document. createtextnode ("-" + val ));

});

</SCRIPT>

</Body>

</Html>

// Output

Mine is one. -1 Mine is two. -2 mine is three. -3-4-5 ++ ++

// Example: --- traverse the array items and pass in the index and Value

<! Doctype HTML>

</SCRIPT> </body>

++ ++

// Example: --- traverses the attributes of the object and passes in the key and Value

<! Doctype HTML>

</SCRIPT> </body>

Example of Self-commenting:

++ ++

1. If you do not want to output the first item (use retrun true) to go to the next Traversal

<! Doctype HTML>

</SCRIPT> </body>

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.