jQuery.prototype.each = function(fn, args) {
return jQuery.each(this, fn, args);
jQuery.each = function(obj, fn, args) {
if (args) {
if (obj.length == undefined) {
for (var i in obj)
fn.apply(obj, args);
} else {
for (var i = 0, ol = obj.length; i < ol; i++) {
if (fn.apply(obj, args) === false)
break;
}
}
} else {
if (obj.length == undefined) {
for (var i in obj)
fn.call(obj, i, obj);
} else {
for (var i = 0, ol = obj.length, val = obj[0];
i < ol && fn.call(val, i, val) !== false;
val = obj[++i]) { }
}
}
return obj;
var arr = ["one", "two", "three", "four", "five"];
var obj = { one: 1, two: 2, three: 3, four: 4, five: 5 };
jQuery.each(arr, function() {
alert(this);
});
/*
one,two,three,four,five
*/
jQuery.each(obj, function(i, val) {
alert(i+":"+val);
});
/*
one:1 two:2 three:3 four:4
five:5
*/
jQuery.each(arr, function(i, val) {
alert(i);
});
/*
0,1,2,3,4
*/
jQuery.each(arr, function(i, val) {
alert(arr);
});
/*
one tow three four five
欢迎光临 PHP开发笔记 (http://phpvi.com/) | Powered by Discuz! 6.1.0 |