问题描述
<table>
<tr><td>foo</td></tr>
<tr><td>bar</td></tr>
<tr><td>[email protected]</td></tr>
</table>
谁能告诉我如何编写一个 Javascript 行来只获取下表中的电子邮件地址,我已经搜索了很多,但我遇到的只是在 td 的任一表中使用id"的教程.. 我想在没有 id 的情况下进行 .. 请帮忙
Can anybody tell me how to write a Javascript line to only grab the email address in the table below, I've been searching a lot, but all I come across is tutorials which use "id" in either table on in td .. I want to do it without having an id .. please help
推荐答案
var rows = document.getElementsByTagName("table")[0].rows;
var last = rows[rows.length - 1];
var cell = last.cells[0];
var value = cell.innerHTML
在这里自己尝试:http://jsfiddle.net/ReyNx/.
显然,您必须更改 document.getElementsByTagName("table")[0]
以适当匹配您的表格
Obviously you'll have to change document.getElementsByTagName("table")[0]
to appropriately match your table
如果您使用 jQuery,那就更容易了:
If you're using jQuery it's easier:
var value = $('table tr:last td').text();
有关详细信息,请参阅 MDN DOM 参考,其中显示了哪些属性是可用于遍历 DOM 的元素.
For more info, see the MDN DOM reference, which shows you which properties are available on which elements to traverse the DOM.
这篇关于Javascript - 获取所有表格 ->tr值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!