For details about how to use the each method in jQuery, jqueryeach

Source: Internet
Author: User

For details about how to use the each method in jQuery, jqueryeach

Overview:

The each () method specifies the function to be run for each matching element.

Returns false to stop the loop as early as possible, which is equivalent to break.

Returns true to end the loop, which is equivalent to continue.

Syntax:

$ (Selector ). each (function (index, element) {}) index-selector index position element-Current element (you can also use the "this" selector) $ (selector ). each (function () {}) $. each (array, function (Key, Value ){})

1. Traverse js Array

$ (Function () {var array = ["aaa", "bbb", "ccc"]; $. each (array, function (I, j) {alert (I + ":" + j); // I indicates the index, j indicates the value });})

2. Traverse Object

Var obj = new Object (); obj. name = "zs"; $. each (obj, function (name, value) {alert (this); // this points to the value of the current attribute, which is equivalent to value alert (name ); // name indicates the name of the current Object attribute alert (value); // value indicates the value of the current Object attribute });

3. Traverse JSON objects

var json ={"name":"zhangSan","role":"student"};$.each(json,function(key,value){ alert(key+":"+value);});

4. Traverse arrays composed of multiple JSON objects

var json =[{"name":"Amy","role":"student"},{"name":"Tom","role":"student"}];$.each(json, function(index, value) {  alert("index="+index+"\n" +"name:"+value.name+"\n"+"role:"+value.role+"\n");});

5. Traverse jQuery objects 

<Head> <meta charset = "UTF-8"/> <title> traverse jQuery objects </title> <script src = "js/jquery-1.12.4.js"> </script> <script type = "text/javascript" >$ (function () {$ ("input [type = 'click']"). bind ("click", function () {$ ("li "). each (function () {alert ($ (this ). text ())});});}); </script> 

Summary

The above is a detailed description of how to use the each method in jQuery. I hope it will be helpful to you. If you have any questions, please leave a message and I will reply to you in time!

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.