javascript 从表格单元格 ID 中获取数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9808987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
get data from table cell ID
提问by HymanTheHyman
I have the following table. What do i need to use in JavaScript to read the number from the below cell named "number"?
我有下表。我需要在 JavaScript 中使用什么来从下面名为“number”的单元格中读取数字?
<table>
<tr>
<td id="number">56656.87</td>
</tr>
</table>
I have tried the following however it does not work.
我已经尝试了以下但它不起作用。
document.getElementById('number');
回答by xandercoded
Using raw JavaScript:
使用原始 JavaScript:
var text = document.getElementById('number').innerText;
Using jQuery:
使用jQuery:
var text = $('#number').text();
回答by Silviu-Marian
The innerHTML
property holds the contents of that cell. If you got [object Object] on your last attempt you're almost there. This is how you do it:
该innerHTML
属性包含该单元格的内容。如果您在上次尝试时获得了 [object Object],那么您就快到了。这是你如何做到的:
var n = document.getElementById('number').innerHTML;
Make sure there's no other number id on that page.
确保该页面上没有其他号码 ID。
回答by Curt
You could use jquery:
你可以使用 jquery:
?$(function(){
alert($("#number").html());
});?