jquery each()函数详解应用
each方法简单的进行了委托:把jquery对象作为第一个参数传递给jquery的each方法.换句话 说:jquery提供的each方法是对参数一提供的对象的中所有的子元素逐一进行方法调用。而jquery对象提供的each方法则是对jquery内 部的子元素进行逐个调用
// args is for internal usage only
each: function( object, callback, args ) {
var name, i = 0,
length = object.length,
isobj = length === undefined || jquery.isfunction(object);
if ( args ) {
if ( isobj ) {
for ( name in object ) {
if ( callback.apply( object[ name ], args ) === false ) {
break;
}
}
} else {
for ( ; i < length; ) {
if ( callback.apply( object[ i++ ], args ) === false ) {
break;
}
}
}
// a special, fast, case for the most common use of each
} else {
if ( isobj ) {
for ( name in object ) {
if ( callback.call( object[ name ], name, object[ name ] ) === false ) {
break;
}
}
} else {
for ( var value = object[0];
i < length && callback.call( value, i, value ) !== false; value = object[++i] ) {}
}
}
return object;
},
从jquery官方找到的一个实例
<!doctype html>
<html>
<head>
<style>
div { color:blue; }
div#five { color:red; }
</style>
<script src="http://code.jquery.com/jquery-1.5.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>
从上面实例可以看得出在回调函数里return false即可,大多数jq的方法都是如此的
===================================
返回 'false' 将停止循环 (就像在普通的循环中使用 'break')。
返回 'true' 跳至下一个循环(就像在普通的循环中使用'continue')。
补充:网页制作,jquery