`
当时我就震惊了
  • 浏览: 32588 次
  • 性别: Icon_minigender_1
  • 来自: 火星
社区版块
存档分类
最新评论

JQuery中的each函数 笔记1

阅读更多
 //函数原型
 jQuery.each( collection, callback(indexInArray, valueOfElement) )
 //collection The object or array to iterate over.
 //collection 需要迭代输出的对象
 
 //callback(indexInArray, valueOfElement) The function that will be executed on every object.
 //callback(indexInArray, valueOfElement) 回调函数,这个函数会对每一个对象执行
    //-- indexInArray 对象索引号/对象属性号
    //-- valueOfElement  对象的值


 //Summary 
A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.
/*
这是一个通用的迭代函数,它可以无缝的迭代输出对象和数组,数组和有一个长度属性数组对象(就像函数中的 参数对象 )可以用它们相应的索引值,从0到length-1 迭代输出,其他的对象(如map对象)可以通过他们的名称属性进行迭代。
*/


//官方秘籍第一句就说了
The $.each() function is not the same as $(selector).each()....
//此 $.each() 非 $(selector).each(), 两者压根不是一回事儿。
//$(selector).each() 参考这里 http://api.jquery.com/each/


//数组
$.each([52, 97], function(index, value) { 
  alert(index + ': ' + value); 
});
//output result:
//0: 52
//1: 97

//Map
var map = { 
  'flammable': 'inflammable', 
  'duh': 'no duh' 
}; 
$.each(map, function(key, value) { 
  alert(key + ': ' + value); 
});
//output result:
//flammable: inflammable
//duh: no duh


We can break the $.each() loop at a particular iteration by making the callback function return false. Returning non-false is the same as a continue statement in a for loop; it will skip immediately to the next iteration.
/*
我们可以跳出$.each()的循环, return false 结束循环,continue 跳过此次循环
*/


<!DOCTYPE html>
<html>
<head>
  <style>
  div { color:blue; }
  div#five { color:red; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
  <div id="one"></div>
  <div id="two"></div>
  <div id="three"></div>
  <div id="four"></div>
  <div id="five"></div>
<script>
    var arr = [ "one", "two", "three", "four", "five" ];
    var obj = { one:1, two:2, three:3, four:4, five:5 };

    jQuery.each(arr, function() {
      $("#" + this).text("Mine is " + this + ".");
       return (this != "three"); // will stop running after "three"
   });

    jQuery.each(obj, function(i, val) {
      $("#" + i).append(document.createTextNode(" - " + val));
    });
</script>

</body>
</html>

结果
Mine is one. - 1
Mine is two. - 2
Mine is three. - 3
- 4
- 5


//另外两个
Example: Iterates over items in an array, accessing both the current item and its index.
$.each( ['a','b','c'], function(i, l){
   alert( "Index #" + i + ": " + l );
 });

Example: Iterates over the properties in an object, accessing both the current item and its key.
$.each( { name: "John", lang: "JS" }, function(k, v){
   alert( "Key: " + k + ", Value: " + v );
 });
分享到:
评论

相关推荐

    JQuery笔记JQuery笔记

    JQuery笔记JQuery笔记JQuery笔记JQuery笔记JQuery笔记JQuery笔记JQuery笔记JQuery笔记JQuery笔记JQuery笔记JQuery笔记JQuery笔记JQuery笔记JQuery笔记JQuery笔记

    Jquery学习笔记Jquery学习笔记

    Jquery学习笔记 Jquery学习笔记 Jquery学习笔记

    jQuery基础教程笔记jQuery基础教程笔记

    jQuery基础教程笔记jQuery基础教程笔记jQuery基础教程笔记jQuery基础教程笔记

    jQuery each函数源码分析_.docx

    jQuery each函数源码分析_.docx

    jQuery 语法学习笔记

    jQuery 语法学习笔记jQuery 语法学习笔记jQuery 语法学习笔记jQuery 语法学习笔记jQuery 语法学习笔记jQuery 语法学习笔记jQuery 语法学习笔记jQuery 语法学习笔记jQuery 语法学习笔记jQuery 语法学习笔记jQuery ...

    Jquery使用each函数实现遍历及数组处理

    主要介绍了Jquery使用each函数实现遍历及数组处理,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

    js JQuery验证电子邮箱的函数

    js JQuery验证电子邮箱的函数

    JQuery各Ajax函数说明

    JQuery各Ajax函数说明。jQuery提供了AJAX全局函数的定义,以满足特殊的需求。下面是jQuery提供的所有函数(按照触发顺序排列如下)

    jquery学习代码和笔记

    本人学习jquery的代码和笔记,主要是jquery的入门知识,以及前后台数据交互。包括一个jquery的web工程和一个配置好的tomcat。jquery工程是用myeclipse10做的,如果eclipse打不开可以重新建工程,再把对应的文件拷贝...

    JQuery笔记

    JQuery笔记JQuery笔记JQuery笔记JQuery笔记JQuery笔记

    jquery的each()详细介绍中文WORD版

    each()函数封装了十分强大的遍历功能,使用也很方便,它可以遍历一维数组、多维数组、DOM, JSON 等等 在javascript开发过程中使用$each可以大大的减轻我们的工作量。有需要的朋友可以下载看看资源截图: 资源...

    jQuery函数全解析

    jQuery函数全解析

    jquery 学习笔记总结

    jquery的学习总结笔记 对选择器和常用方法进行总结 有实例

    jquery的each()函数用法[文].pdf

    jquery的each()函数用法[文].pdf

    jQuery each函数源码分析

    jQuery.each方法用于遍历一个数组或对象,并对当前遍历的元素进行处理...* each函数通过jQuery.extend函数附加到jQuery对象中: * jQuery.extend({ * each: function() {} * }); * 如果对jQuery.extend函数源码还不了解,

    jQuery 对象和函数列表

    这里是我从Jquery中整理出来的一些对象和函数的用法,加上我自己的一些理解,和各位分享一下

    JQuery学习笔记

    JQuery学习笔记 JQuery学习笔记 JQuery学习笔记 JQuery学习笔记 JQuery学习笔记

    jquery里的each使用方法详解

    换句话 说:jQuery提供的each方法是对参数一提供的对象的中所有的子元素逐一进行方法调用。而jQuery对象提供的each方法则是对jQuery内 部的子元素进行逐个调用。 这个JQUERY里的核心代码 代码如下: jQuery.prototype...

    jQuery操作函数

    内容包括:jQuery Ajax 操作函数,jQuery 选择器,jQuery 事件处理方法,jQuery 事件方法,jQuery 效果函数,jQuery 文档操作方法,jQuery 属性操作方法,jQuery CSS 操作函数,jQuery 遍历函数

    jqueryAPI函数chm文档

    对jquery中api函数的描述,十分详细,是开发者所必备的chm文档

Global site tag (gtag.js) - Google Analytics