我期待创建使用jQuery从表一中<thead>
阵列和<tbody>
使用jQuery
我正在寻找的结果是
[[20th Jan, 33], [21st Jan, 44], [22nd Jan, 5],[23rd Jan, 17]]
我的表是创建一个从表数组如下
<table class="" id="bookedTable" border="1">
<thead>
<tr>
<th>Date</th>
<th>20th jan</th>
<th>21st jan</th>
<th>22nd jan</th>
<th>23rd Jan</th>
</tr>
</thead>
<tbody>
<tr>
<td>Name</td>
<td>33</td>
<td>44</td>
<td>5</td>
<td>17</td>
</tr>
</tbody>
我的JS是如下
$(function() {
var $table = $("#bookedTable"),
$headerCells = $table.find("thead th"),
$rowCells = $table.find("tbody tr td");
var headers = [],
rows = [];
$headerCells.each(function(k,v) {
headers[headers.length] = $(this).text();
});
$rowCells.each(function(k,v) {
rows[rows.length] = $(this).text();
});
console.log(headers);
console.log(rows);
});
我只能找出如何控制台日志的标题和行sepeartely和1个阵列按我希望的结果上面没有将它们结合起来。寻找一些帮助来解决。
非常好的代码,但一个建议。你可以将索引作为第二个参数传递给'map'函数,从而不需要'$(el).index()' –
@KevinBowersox - 这是真的,在我输入时没有想到。添加! – adeneo
非常感谢,这是一个很好的代码。我刚刚意识到的一件事是我的代码现在正在发布,因为没有返回日期对象。是否可以在数组中(对不起,我知道这是一个附加),你可以返回th中的日期字段作为数组中的日期对象吗?感谢和抱歉,后期跟进.. – Redwall