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