• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

Javascript-获取所有表格-和amp;gt; tr值

用户头像
it1352
帮助1

问题说明

<table>
  <tr><td>foo</td></tr>
  <tr><td>bar</td></tr>
  <tr><td>abc@yahoo.com</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

正确答案

#1
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.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcfgika
系列文章
更多 icon
同类精品
更多 icon
继续加载