1. How to use map in jquery
<! DOCTYPE html>"Text/javascript"Src="/jquery/jquery.js"></script>"Post"action=""> <fieldset> <div> <label for=" Both">2</label> <input type="checkbox"Value="2"Id=" Both"Name="number[]"> </div> <div> <label for=" Four">4</label> <input type="checkbox"Value="4"Id=" Four"Name="number[]"> </div> <div> <label for="Six">6</label> <input type="checkbox"Value="6"Id="Six"Name="number[]"> </div> <div> <label for="Eight">8</label> <input type="checkbox"Value="8"Id="Eight"Name="number[]"> </div> </fieldset></form><p><b>values: </b></p><script> $("P"). Append ($ (': CheckBox'). Map (function () {return This. ID; }).Get(). Join (','));</script></body>jquery invokes a function object for each element in the current collection, using the notation
. Map (Callback (index,element))
2. The Foreach method
This method can only be used in non-IE browser, ie needs to write a support, applied to the array of traversal, call each element in the array
Array.foreach (callback[, Thisobject]);
if(!Array.prototype.forEach) {Array.prototype.forEach= function (fun/*, Thisp*/) { varLen = This. Length; if(typeofFun! ="function") Throw NewTypeError (); varThisp = arguments[1]; for(vari =0; i < Len; i++) { if(Iinch This) Fun.call (Thisp, This[i], I, This); } };}
function PRINTBR (element, index, array) {///array element, element index, array itself document.write ("<br/>[ " " " + Element);} [58].foreach (PRINTBR);
Here is the sample code
Here's a support method.
Note: foreach cannot terminate before all elements are passed to the called function (whereas for loops have a break method), and if you want to terminate prematurely, you must put foreach in the try block and throw an exception. If the function called by foreach () throws a Foreach.break exception, the loop terminates prematurely: (You can also consider jumping out with If+return)
foreach (a,b,c) {try{a.foreach (b,c);} Catch (e) {if(e===foreach. break)return; Else Throw e;}} foreach. break =new Error ("stopiteration");}
3. For in
Can be used to iterate over an array, or to iterate over an object
var a = {"first":1,"second":2, " Third ":3};
In this form, you can iterate through the
for (var in a) { alert (a[x]);}
The traversal method of jquery