最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - get data from table cell ID - Stack Overflow

programmeradmin1浏览0评论

I have the following table. What do i need to use in JavaScript to read the number from the below cell named "number"?

<table>
<tr>
<td id="number">56656.87</td>
</tr>
</table>

I have tried the following however it does not work.

document.getElementById('number');

I have the following table. What do i need to use in JavaScript to read the number from the below cell named "number"?

<table>
<tr>
<td id="number">56656.87</td>
</tr>
</table>

I have tried the following however it does not work.

document.getElementById('number');
Share Improve this question asked Mar 21, 2012 at 16:39 JackTheJackJackTheJack 1871 gold badge1 silver badge10 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 7

Using raw JavaScript:

var text = document.getElementById('number').innerText;

Using jQuery:

var text = $('#number').text();

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:

     var n = document.getElementById('number').innerHTML;

Make sure there's no other number id on that page.

You could use jquery:

​$(function(){
   alert($("#number").html()); 
});​

http://jsfiddle/fG5nF/

If you use jQuery, you can just do this:

var number = $("td#number").text();
发布评论

评论列表(0)

  1. 暂无评论