我想使用
var merch = document.getElementById('merch');
检索我的网页上的表格,该表格是动态填充的。然后我想迭代表格,一次一行,捕获
<td>
元素并将它们每个作为字符串存储在数组中。每行都有自己的数组。
有人可以告诉我如何实现这一目标吗?我确信有一种简单的方法我在搜索中还没有找到。
预先感谢您的考虑。
最佳答案
var merch = document.getElementById('merch');
// this will give you a HTMLCollection
var rows = merch.rows;
// this will change the HTMLCollection to an Array
var rows = [].slice.call(merch.rows);
// if you want the elements in the array be string.
// map the array, get the innerHTML propery.
var rows = [].slice.call(merch.rows).map(function(el) {
return el.innerHTML;
});
关于javascript - 按 ID 抓取表并一次迭代一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12417363/