jQuery 遍历List集合
<!doctype html> <html> <head> <meta charset="utf-8"> <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script> <script type="text/javascript "> $(function() { var tbody = ""; var obj = [{ "name": "项海军", "password": "123456" }, { "name": "西门庆", "password": "333333" } ]; $("#result").html("遍历List集合.each的使用"); //下面使用each进行遍历 $.each(obj, function(n, value) { alert(n + ' [' + value.name + ' ' + value.password + ']'); var trs = ""; trs += " <tr> <td> " + value.name + " </td> <td>" + value.password + "</td > </tr>"; tbody += trs; }); $("#project").append(tbody); }); </script> </head> <body> <div id="result" style="font-size:16px;color:red;"></div> <table cellpadding="5" cellspacing="1" width="6" id="project" border="1"> <tr> <th> 用户名</th> <th>密码</th> </tr> </table> </body> </html>
效果图:
说明:
jQuery.each() 函数用于遍历指定的对象和数组。
语法
$.each( object, callback )
更多web前端知识,请查阅 HTML中文网 !!