Use of the jQuery each function

Source: Internet
Author: User

The each function of jQuery is primarily used to iterate through the collection. such as the node collection of XML data, object collections, and so on. Let's look at the node collection for each function that traverses the XML.

The first is the data in the XML format returned by the Java backend, with the following code:

Out.println ("<StudentId=\ "stu\" > "); Out.println ("<name>"); Out.println ("<firstName>"); Out.println ("Wkl0"); Out.println ("</firstName>"); Out.println ("</name>"); Out.println ("<name>"); Out.println ("<firstName>"); Out.println ("WKL1"); Out.println ("</firstName>"); Out.println ("</name>"); Out.println ("<sex>"); Out.println (M); Out.println ("</sex>"); Out.println ("<sex>"); Out.println (W); Out.println ("</sex>"); Out.println ("</student> ");

The jquery code is as follows:

Each use: the role of traversal. Index represents the current index, ele represents the traversal of the current object, and must be wrapped up in $ () to be a jquery object, so that jquery can be used to provide a way to parse the XML var jqobj = $ (dat      a); Convert the DOM (XML) object to the JQuery object Jqobj.find ("Student"). Find ("name"). each (function (Index,ele) {//traverse the name node. You can search for name directly, or you can find the name by using the parent node of name//var firstName = $ (ele). Find ("FirstName"); Get the value in two waysvar FirstName = $ (this). Children ("FirstName");                       Two ways to get the value of var firsttext = Firstname.text ();                 alert (Firsttext); });

Traverse all the sex nodes
Jqobj.find ("Sex"). each (function () {
Because the sex node here is a no other node contained inside, so directly can get its node value. This represents the current object (node).
var Sextext = $ (this). text ();//No more. Find ().
alert (Sextext);
});

Use of the jQuery each function

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.