jquery uses $.each to traverse the JSON array with a simple implementation method _jquery

Source: Internet
Author: User

The example in this article describes the simple implementation of jquery using $.each to traverse the JSON array. Share to everyone for your reference, specific as follows:

<!doctype HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en"
"http://www.w3.org/tr/xhtml1/dtd/ Xhtml1-transitional.dtd ">
 
 

The $ (). each () method of the JQuery object, which can be used to sample any object

The callback function has two parameters:

The first is the index of the member or array of the object

Examples of arrays, using element indexes and content

$.each ([0,1,2], function (index, content) {
 alert ("Item #" + Index + "Its value is:" + content);
});

The second one is the corresponding variable or content

If you need to exit each loop so that the callback function returns false, the other return values are ignored.

Jquery.each (Obj,fn,arg)

The method has three parameters: the object obj to operate, the function FN to operate, and the function's parameter args.

1.obj objects are arrays

Each method invokes the FN function of the neutron element of the array, until the result returned by a call to a child element is false, that is, we can exit each method call when the supplied FN function is processed so that it satisfies a certain condition. When the arg argument is supplied by the each method, the argument passed in by the FN function is arg, otherwise: the child element Index, the child element itself

2.obj object is not an array

The biggest difference between this method and 1 is that the FN method is carried out without regard to the return value. In other words, all the properties of the Obj object are invoked by the FN method, even if the FN returns false. Calling incoming arguments is similar to 1.

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;
 


   

More interested readers of jquery-related content can view the site topics: "jquery operations JSON Data Tips Summary", "jquery switching effects and techniques summary", "jquery drag and drop effects and tips summary", "JQuery Extended Skills Summary", " jquery Common Classic Effects Summary "jquery animation and special effects usage Summary", "jquery selector usage Summary" and "jquery common Plug-ins and Usage summary"

I hope this article will help you with the jquery program design.

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.