jquery $.each Data Traversal usage

Source: Internet
Author: User

$.each (Array, [callback]) traversal [common]
Explanation: Unlike the $.each () method of the JQuery object, this method can be used to sample any object (not just the array oh ~). 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. If you need to exit each loop so that the callback function returns false, the other return values are ignored.
Each traversal, believed to be new, is a variant of the For loop in the usual event processing, but is more powerful than the for loop. In an array, it can easily take the array index and its corresponding value. Example:

var _mozi=[' mohist ', ' Mo zi ', ' Modi ', ' love not attack ', ' still with Shangxian '];//the array used in this article
$.each (_mozi,function (key,val) {
The callback function has two parameters, the first is the element index, and the second is the current value
Alert (' _mozi array, index: ' +key+ ' corresponds to the value: ' +val ');
});


In relation to the native for.. In,each a little stronger. For.. In can also traverse the array and return the corresponding index, but the value is to be obtained through Arrname[key];

<script>
$ (function ()) {
var _mozi=[' mohism ', ' Mozi ', ' Modi ', ' and ' love not attack ', ' still with Shangxian '];
 //Gets the collection of objects for the H2 element using native getElementsByTagName
 var h2obj=document.getelementsbytagname (' H2 ');
 
 //$.each () traverses the array
 $ (' Input#js_each '). Click (function () {
  $.each (_mozi, function (key,val) {
   //callback function has two parameters, the first is an element index, the second is the current value
   alert (' _mozi array, index: ' +key+ ' corresponding value is: ' +val ';
  });
 });
}
</script>

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.