//例子:???传入数组
$.each([52, 97], function(index, value) {
alert(index + ‘: ‘ + value);
});
《script》
//输出
0: 52
1: 97
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//例子:???如果一个映射作为集合使用,回调函数每次传入一个键-值对
var map = {
‘flammable’: ‘inflammable’,
‘duh’: ‘no duh’
};
$.each(map, function(key, value) {
alert(key + ‘: ‘ + value);
});
《script》
//输出
flammable: inflammable
duh: no duh
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++内容来自17jquery
//例子:???回调函数中 return false时可以退出$.each(), 如果返回一个非false即会像在for循环中使用continue 一样, 会立即进入下一个遍历
div { color:blue; }
div#five { color:red; }
<scriptsrc=”http://code.jquery.com/jquery-latest.js”>《script》
《script》
var arr = [ "one", "two", "three", "four","five" ];//数组
var obj = { one:1, two:2, three:3, four:4,five:5 }; // 对象 17jquery.com
jQuery.each(arr, function() { // this指定值
$(“#” + this).text(“Mine is ” +this + “.”); // this指向为数组的值, 如one, two
return (this != “three”);// 如果this = three 则退出遍历
});
jQuery.each(obj, function(i, val) { //i 指向键, val指定值
$(“#” +i).append(document.createTextNode(” C ” + val));
});
《script》
// 输出
Mine is one. C 1
Mine is two. C 2
Mine is three. C 3
- 4
- 5
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
//例子:???遍历数组的项, 传入index和value
一起jquery,17jquery
$.each( ['a','b','c'], function(i, l){
alert( “Index #” + i + “: ” + l );
});
《script》