JS native foreach, map and jquery each, $.each difference

Source: Internet
Author: User

1<! DOCTYPE html>23 45<meta charset= "UTF-8" >6<title>rem Phone test</title>7<meta name= "viewport" content= "User-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" >8<style>9*{margin:0;padding:0}Ten body{ Onefont-size:12px; A         } - p{ -font-size:14px; the         } - . Demo{width:rem;height:20rem;background: #00f} - ul LI{WIDTHL} -</style> +  - +  A<body> at  -<div class= "Demo" > -<ul> -<li></li> -<li></li> -<li></li> in<li></li> -<li></li> to</ul> +</div> -<script src= "Http://libs.useso.com/js/jquery/1.8.3/jquery.min.js" ></script> the<script> * varA = [1,1,1,1,1,1,1]; $ //the parameter order of the foreach and map is exactly the opposite of the jquery each $.each order , and the order of JS is the first element and then the indexPanax NotoginsengA.foreach (function(element,index) { - Console.log (element); the }); + varb = A.map (function(element,index) { A    return2 the }); + Console.log (b); - //The order of jquery is first index and then element, and $.each can pass other parameters besides passing index and element . $ //index and element will fail; $ //Note that when you use. Each, you need to convert the array to a jquery array; -$(function(){ -$ (a). each (function(index,element) { the Console.log (Element) -     });Wuyi  the$.each (A,function(index,element) { - Console.log (element); Wu //If you need to operate the jquery method on element, you need to write it in this way and convert it to a jquery object; - //Console.log ($ (element)) About     }); $  - //$.each Pass other parameter usage; -$.each (A,function(e1,e2,e3) { - Console.log (E2); A  +},[11,22,33]) the }); -  $</script> the</body> the  the

Related articles:

Http://www.cnblogs.com/mabelstyle/archive/2013/02/19/2917260.html

JS native foreach, map and jquery each, $.each difference

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.