jQuery_练习将表格内容转换为对象数组

如题
 1 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
 2 "http://www.w3.org/TR/html4/strict.dtd">
 3 
 4 <html xmlns="http://www.w3.org/1999/xhtml" lang="en">
 5     <head>
 6         <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 7         <title>Jquery_属性操作</title>
 8         <meta name="author" content="Administrator" />
 9         <script type="text/javascript" src="jquery-1.11.1.js"></script>
10         <script type="text/javascript">
11             
12             $(function(){
13                 //属性名
14                 var props = ["num","name","class","grade"];
15                 var objs = new Array();
16                 
17                 $("tbody tr").each(function(){
18                     var obj = new Object();
19                     $(this).find("td").each(function(n){
20                         obj[props[n]] = $(this).html();
21                     });
22                     objs.push(obj);
23                 });
24                 
25                 for(var i=0;i<objs.length;i++){
26                     alert(objs[i]["num"+ " -- " + objs[i]["name"+ " -- " + objs[i]["class"+ " -- " + objs[i]["grade"]);
27                 }
28                 
29             });
30             
31         </script>
32         <!-- Date: 2014-11-23 -->
33     </head>
34     <body>
35         <table width="500px" height="300px" cellpadding="0px" cellspacing="0px" border="1px solid black">
36             <thead>
37                 <tr><th>编号</th><th>姓名</th><th>班级</th><th>年级</th></tr>
38             </thead>
39             <tbody>
40                 <tr><td>01</td><td>duyt111</td><td>01class</td><td>01grade</td></tr>
41                 <tr><td>02</td><td>duyt222</td><td>02class</td><td>02grade</td></tr>
42                 <tr><td>03</td><td>duyt333</td><td>03class</td><td>03grade</td></tr>
43                 <tr><td>04</td><td>duyt444</td><td>04class</td><td>04grade</td></tr>
44                 <tr><td>05</td><td>duyt555</td><td>05class</td><td>05grade</td></tr>
45                 <tr><td>06</td><td>duyt666</td><td>06class</td><td>06grade</td></tr>
46             </tbody>
47         </table>
48     </body>
49 </html>
50 

posted on 2014-11-25 21:03 都较瘦 阅读(2049) 评论(1)  编辑  收藏 所属分类: Jquery案例积累

评论

# re: jQuery_练习将表格内容转换为对象数组 2016-08-02 11:48 等我去而定

带我去的期望  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2014年11月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

统计

公告

博客定位:囿于目前的水平,博客定位在记录自己的学习心得和随手的练习

常用链接

留言簿

随笔分类

随笔档案

文章分类

文章档案

搜索

最新评论

阅读排行榜

评论排行榜