js 将表格的数组存储为二维数组
表格数据:
<table id="tb" width="100%" border="1" cellspacing="3" cellpadding="3" style="text-align: center; display: none;"> <caption style="margin-bottom: 15px">商家端功能权限列表</caption> <tr> <!--角色--> <th style="width: 30px !important;">功能模块显示</th> <th>seller-M</th> <th>seller-E</th> <th>zone-P</th> <th>city-P</th> <th>seller-H-R</th> <th>advertiser</th> </tr> <!--功能--> <tr> <th>昨日销售</th> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>0</td> <td>0</td> </tr> <tr> <th>商家余额</th> <td>1</td> <td>1</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> </tr> <tr> <th>待配送</th> <td>1</td> <td>1</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> </tr> <tr> <th>营业状态</th> <td>1</td> <td>1</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> </tr> <tr> <th>配送中</th> <td>1</td> <td>1</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> </tr> <tr> <th>商城销售</th> <td>1</td> <td>1</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> </tr> <tr> <th>收支流水</th> <td>1</td> <td>0</td> <td>1</td> <td>1</td> <td>0</td> <td>1</td> </tr> <tr> <th>分类管理</th> <td>1</td> <td>1</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> </tr> <tr> <th>商品管理</th> <td>1</td> <td>1</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> </tr> <tr> <th>发红包</th> <td>1</td> <td>0</td> <td>1</td> <td>1</td> <td>0</td> <td>1</td> </tr> <tr> <th>优惠券</th> <td>1</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> </tr> <tr> <th>数据报表</th> <td>1</td> <td>1</td> <td>1</td> <td>0</td> <td>0</td> <td>0</td> </tr> <tr> <th>我的店铺</th> <td>1</td> <td>1</td> <td>0</td> <td>0</td> <td>0</td> <td>0</td> </tr> <tr> <th>意见反馈</th> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>0</td> <td>1</td> </tr> <tr> <th>联系我们</th> <td>1</td> <td>1</td> <td>1</td> <td>1</td> <td>0</td> <td>1</td> </tr> <tr> <th>房屋验证</th> <td>0</td> <td>0</td> <td>0</td> <td>0</td> <td>1</td> <td>0</td> </tr> </table>
js关键代码
// 权限列表生成二维数组 var tr = document.getElementsByTagName('tr'); var td = document.getElementsByTagName('td'); var arr2 = new Array(); // 初始化二维数组 for(var i = 0; i < tr.length-1;i++){ arr2[i] = new Array(); for(var j = 0; j < td.length/(tr.length-1); j++){ arr2[i][j] = ''; } } // console.log(arr2.length); // 生成权限列表二维数组 for (var x = 0; x < tr.length-1; x++) { for (var y = 0; y < td.length/(tr.length-1); y++) { arr2[x][y] = td[x*(td.length/(tr.length-1))+y].innerText; console.log('arr2['+x+']['+y+'] == '+ arr2[x][y] ) } }
alert (arr2)