jquery each () function detailed application

Source: Internet
Author: User

The

Each method is simply delegated: The jquery object is passed as the first argument to the each method of jquery. In other words: jquery provides a method call for each of the child elements in the object provided by the parameter one. The jquery object provides each method to call the child elements within jquery individually

Args is to 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;
},

An example from the jquery official found

<!doctype html>
<style>
div {color:blue;}
div#five {color:red;}
</style>
<script src= "Http://code.jquery.com/jquery-1.5.js" ></script>
<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 are" + this + ".");
Return (This!= "three"); Would stop running after "three"
});

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

</body>

From the example above you can see that return false in the callback function, most JQ methods are the same

===================================
Returning ' false ' will stop the loop (like using ' break ' in an ordinary loop).
Returns ' true ' to the next loop (like using ' Continue ' in an ordinary loop).

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.