each () function of jquery

Source: Internet
Author: User

1, $.each ()--Global function introduction

Syntax: global function Jquery.each (collection,function (indexinarray,valueofelement))

collection--can be any collection, whether it is a JS object or an array, or a JSON object.


The key of the indexinarray--object or the index of the array


valueofelement--the value of an object or an item of an array

Traversing a one-dimensional array
  <script>    var arr = [' A ', ' B '];    Jquery.each (arr,function(index,value) {      console.log (index);      Console.log (value);    })   </script>

Results:

Traversing a two-dimensional array
  <script>    var arr = [[' A ', ' B '],[' C ', ' d '];    Jquery.each (arr,function(index,value) {      Jquery.each (value,function(i, Element) {        console.log (i);        Console.log (Element);})    })   </script>

Results:

Traversing objects
  <script>    var obj = {      ' Zs ',      +    };    Jquery.each (obj,function(index,value) {      console.log (index);      Console.log (value);    })   </script>

Results:

Traversing JSON objects
var obj = {"A": "Hello", "B": "World"}; Jquery.each (obj,function(index,value) {    Console.log (index);    Console.log (value);})

Results:

1, $ (selector). each ()
Syntax: $ (selector). Each (function(index,value))
index--the index of a pseudo-array
value--the item value of a pseudo-array
Dealing with more of the above on the DOM
Traverse the LI Tag
  <ul>    <li>1</li>    <li>2</li>    <li>3</li>    <li>4 </li>    <li>5</li>  </ul>  <script>    $ (' Li '). each (  function(index,value) {      console.log (index);       /* print a string of shape <li>1</li> */       Console.log (value);       /* If you want to output data from a tag, you can look at the bar as a DOM object      . Then use the innerHTML method to output the data in the tag, or you can convert it to a      jquery object shaped like $ (value). HTML (), and then      the printout, as follows:*/       Console.log (value.innerhtml);    })   </script>

Summary
    1. Traverse data: Jquery.each (Collection,function (indexinarray,valueofelement));

    2. Traverse dom:$ (selector). each (function (index,value));

each () function of jquery

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.