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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 07:55:03  来源:igfitidea点击:

get data from table cell ID

javascript

提问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 innerHTMLproperty 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()); 
});?

http://jsfiddle.net/fG5nF/

http://jsfiddle.net/fG5nF/

回答by Constantino Tsarouhas

If you use jQuery, you can just do this:

如果你使用jQuery,你可以这样做:

var number = $("td#number").text();