我正在尝试解析 HTML 表:
<table id="compare_multi" class="" data-intro="">
<thead class='header'>
<tr>
<th colspan="2">
<span class="pull-right"></span>
</th>
<th class="center">
</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th class="category" colspan="" style="padding: 10px;"></th>
</tr>
<tr class="valuerow">
<td style="width: 25px">122</td>
<td></td>
@foreach
{
<td class="center tdvalue">INIB</td>
<td class="center tdvalue">4</td>
<td class="center tdvalue">4</td>
<td class="center tdvalue">INIB</td>
}
<td></td>
</tr>
</tbody>
</table>
Jquery:
var row = [];
$("#compare_multi tr").each(function()
{
$this = $(this);
var tdvalue = $this.find(".tdvalue").text();
row.push(tdvalue);
});
console.log(row);
我正在尝试解析表格:我得到如下结果
[“INIB 4 4 INIB”、“12 4 9 1”、“2 2 2 INIB”]
我希望它是这样的:
Array 1:
0: INIB
1: 4
2: 4
3: INIB
Array 2:
.
.
.
.
.
我做错了什么? 请帮忙
最佳答案
您的代码的问题是 $this.find(".tdvalue").text()
返回一个字符串,它是所有 ".tdvalue"
文本,而不是您期望的数组。您应该对此数组执行单独的 .each()
操作,以便将分隔的 td
值放入数组中(对于每个 tr
> 在你的表中。
考虑到您的 HTML,您的意思可能是这样的:
var rows = []; // the result array
$("#compare_multi tr").each(function() {
$tr = $(this);
var row = []; // array for each tr
$tr.find(".tdvalue").each(function(){
row.push($(this).text()); //separate value for each tdvalue
});
rows.push(row);
});
console.log(rows);
关于javascript - Jquery HTML 表本身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38475689/