JavaScript将HTML数字表解析为数组 [英] JavaScript to parse HTML table of numbers into an array

查看:107
本文介绍了JavaScript将HTML数字表解析为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将网页上的现有HTML表格解析为数字数组,以便稍后传递给绘图对象。我正在学习JavaScript,我不清楚我应该如何迭代HTML标签中的数据值。这就是我想出的:

I'm parsing an existing HTML table on a webpage into an array of numbers to later pass to a plot object. I'm learning JavaScript and it is not clear how I am supposed to iterate over the data values in HTML tags. This is what I've come up with:

for (i = 0; i < table.rows.length; i += 1) {
   row = table.rows[i];
   for (j = 0; j < row.cells.length; j += 1) {
       cell = row.cells[j];
       coord[j] = Number(cell.innerText);
   }
   data[i] = coord.slice();
}

我对.innerText部分感到困扰。这是迭代< td> 标签中文本元素的通用机制吗?

I'm bothered by the .innerText part. Is that the universal mechanism for iterating over the text elements in the <td> tags?

推荐答案

不幸的是 .innerText 不是通用的,最值得注意的是它在Firefox中缺失。如果它只是单元格中的文本或 Number(cell.innerText || cell.textContent),请使用 .innerHTML 占所有浏览器的帐号。

Unfortunately .innerText is not universal, most notably it's missing from Firefox. Use either .innerHTML here if it's just text in the cell or Number(cell.innerText || cell.textContent) to account for all browsers.

这篇关于JavaScript将HTML数字表解析为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆