Examples of each traversal objects and arrays in jquery _ jquery

Source: Internet
Author: User
In jquery, each can be used to traverse objects and arrays. to exit the each loop, the callback function returns false. The following example shows a general Traversal method, it can be used to traverse objects and arrays. $ (). Each (), the callback function has two parameters:

The first is the index of the member or array of the object, and the second is the corresponding variable or content. To exit the each loop, the callback function returns false.

The following two select statements are available:

Plan category: -All- New Continued construction Application Type: -All- Declaration Modify 

Use the each method to obtain the text value in option, that is,-all-, new, and continued creation...

If you only use an each loop once, you can start from option.

$ ("Option "). each (function (index, data) {console.info ($ (data ). text (); // or console.info ($ (this ). text ());})

It can also start at select.

$("select").each(function( index,data){ $("option", data).each(function(m,n){ console.info($(this).text()); }) })

$ ("Option", data) must be added with data or $ ("option", this), indicating the option under this object

Otherwise, all options are used.

-------------------------- Gorgeous split line -----------------------------

Another method of each is jQuery. each (object, [callback]).
Unlike the $ (). each () method of the jQuery object, this method can be used to sample any object.

Use this method to traverse the above Code

$.each($("option"),function(index,data){ console.info(index+" "+data); })

You can also traverse the array.

$.each( [0,1,2], function(i, n){ console.info( "Index:" + i + ": " + n ); });

Traverse objects

$.each({ name: "itmyhome", addr: "Beijing" },function(i, n){ console.info("Name: " + i + ", Value: " + n); });
Related Article

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.