$.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>