jquery's $ (selector). The difference between Each,$.each

Source: Internet
Author: User

Recently, I have been studying JS, and today we see the function of traversing the module:

$ (selector). each (function ())

But consider that this function is not the same as the function used in the previous project to traverse the data (the function used in the project: $.each (Dataresource,function (index,element))), so, to study the next, Sure enough in JS there are two similar functions, so there is today's theme:

1.$ (selector). Each (function ()) 
2.$.each (Dataresource,function (index,element))

This is followed by an in-depth discussion of these two functions:

1.$ (selector). each (function ())

Role: used more in DOM processing

Example :

HTML Partial Document

<ul id= "each_id" >
<li>Coffee</li>
<li>Soda</li>
<li>Milk</li>
</ul>

JS Traversal function:

function Traversaldom () {
$ ("#each_id Li"). each (function () {
Alert ($ (this). Text ())
});
}

Output Result:

2.$.each (Dataresource,function (index,element))

Function: use more in data processing

Example:

There is no HTML code, only JS code, as follows:

function Traversaldata () {
var jsonresourcelist = ' [{' id ': ' 1 ', ' tagName ': ' Apple '},{' id ': ' 2 ', ' tagName ': ' Orange '},{' id ': ' 3 ', ' tagName ': ' Banan a "},{" id ":" 4 "," TagName ":" Watermelon "}] ';
if (Jsonresourcelist.length >0) {
$.each (Json.parse (jsonresourcelist), function (index, obj) {
alert (obj.tagname);
});
}
}

Output Result:

3. Final conclusion:

You typically use $ (selector) when traversing the DOM. each (function ()) functions;

The $.each (Dataresource,function (index,element)) function is typically used when traversing data.

OK, let's go here, hope can help everyone!

jquery's $ (selector). The difference between Each,$.each

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.