原型 - 获取表格单元格内的值 [英] Prototype - get value inside table cell

查看:55
本文介绍了原型 - 获取表格单元格内的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码段(Prototype):

$('table_cell_id')

那个单元格包含一个数字.我如何将这个数字放入 JavaScript 变量中?

That cell contains a number. How would I get this number into a JavaScript variable?

推荐答案

如果单元格只包含文本,应该这样做:

If the cell only contains text something like this should do:

var someNumber, stringData = $("table_cell_id").innerHTML.strip();  
if (stringData.length > 0) someNumber = Number(stringData);
if (isNaN(someNumber)) {
  alert("Error: someNumber is not a number");
}
else {
  alert(someNumber);
}

innerHTML 返回单元格的原始字符串数据.strip() 删除前导和尾随空格,Number(x) 将修剪后的字符串转换为数字.

innerHTML returns raw string data of the cell. strip() removes leading and trailing whitespaces and Number(x) casts the trimmed string to a number.

当然,如果单元格可能是空的,您就不需要检查了.另外,最好检查变量是否为 NaN(不是数字).

If the cell could be empty you wan't to check for that, of course. Also, it is good to check whether the variable is NaN (not a number).

这是一个应该有效的单行:

Here's a one-liner that should work:

var someNumber = Number($("table_cell_id").innerHTML.strip());

这篇关于原型 - 获取表格单元格内的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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