The traversal method of jquery

Source: Internet
Author: User

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

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.