JQuery universal global Traversal method $. each () usage example, jquery. each
This example describes the general global Traversal method $. each () in jQuery. We will share this with you for your reference. The details are as follows:
1. test. json file code:
[{"Username": "zhangsan", "content": "sofa. "},{" username ":" Li Si "," content ":" bench. "},{" username ":" Wang Wu "," content ":" floor. "}]
2.html code:
<P> <input type = "button" id = "send" value = "LOAD"/> </p> <div class = "comment"> existing comments: </div> <div id = "resText"> </div>
3. jQuery code:
<Script src = "jquery-1.3.1.js" type = "text/javascript"> </script> <script type = "text/javascript">/* 1. $. each () is a common Traversal method of jquery. It can be used to traverse objects and arrays. $. the each () function is different from the each () method of the jquery object. It is a global function that does not operate the jquery object, but uses an array or object as the first parameter, use a callback function as the second parameter. The callback function has two parameters: the first parameter is the index of the object member or array, and the second parameter is the corresponding variable or content */$ (function () {$ ('# send '). click (function () {$. getJSON ('test. json', function (data) {$ ('# resText '). empty (); var html = ''; $. each (data, function (commentIndex, comment) {html + = '<div class = "comment">