3 回答
TA贡献1859条经验 获得超6个赞
表内容转数组
var myTableData = Array.prototype.map.call(document.querySelectorAll('#myTable tr'), function(row){
return Array.prototype.map.call(row.querySelectorAll('td'), function(td){
return td.innerHTML;
});
});
console.log(myTableData)
<table id="myTable">
<tr>
<td>1</td>
<td>a</td>
</tr>
<tr>
<td>2</td>
<td>b</td>
</tr>
<tr>
<td>3</td>
<td>c</td>
</tr>
</table>
TA贡献1842条经验 获得超21个赞
这是ES6版本
const myTableData = [...document.querySelectorAll('#myTable tr')]
.map(row => [...row.querySelectorAll('td')]
.map(cell => cell.textContent));
console.log(myTableData)
<table>
<tbody id="myTable">
<tr>
<td>1</td>
<td>a</td>
</tr>
<tr>
<td>2</td>
<td>b</td>
</tr>
<tr>
<td>3</td>
<td>c</td>
</tr>
</tbody>
</table>
TA贡献1833条经验 获得超4个赞
我假设您的问题中定义的问题行是标题。您可以使用querySelectorAllindocument获取所有行,然后将其映射到数组中。希望下面的代码有帮助:
function getAllProblemRowElements() {
const tableContents = document.querySelectorAll("#app > div > div > div > div.table-responsive > table > tbody > tr");
const sources = Array.from(tableContents,source=>source.children[2].getAttribute("value"))
return sources;
}
添加回答
举报