Javascript - 获取所有表 -> tr 值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13763111/
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
Javascript - get all table -> tr values
提问by Irfan Harun
<table>
<tr><td>foo</td></tr>
<tr><td>bar</td></tr>
<tr><td>[email protected]</td></tr>
</table>
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
谁能告诉我如何编写一个 Javascript 行来只获取下表中的电子邮件地址,我一直在搜索很多,但我遇到的只是在 td 的任一表中使用“id”的教程..我想在没有身的情况下做到这一点..请帮忙
回答by Matt
var rows = document.getElementsByTagName("table")[0].rows;
var last = rows[rows.length - 1];
var cell = last.cells[0];
var value = cell.innerHTML
Try it yourself here: http://jsfiddle.net/ReyNx/.
在这里自己尝试:http: //jsfiddle.net/ReyNx/。
Obviously you'll have to change document.getElementsByTagName("table")[0]to appropriately match your table
显然你必须改变document.getElementsByTagName("table")[0]以适当地匹配你的桌子
If you're using jQuery it's easier:
如果您使用 jQuery,则更容易:
var value = $('table tr:last td').text();
For more info, see the MDN DOM reference, which shows you which properties are available on which elements to traverse the DOM.
有关更多信息,请参阅MDN DOM 参考,其中显示了哪些属性可用于遍历 DOM 的元素。
回答by Cerbrus
No jQuery, innerHtmlor other evil / heavy functions, just plain old JavaScript:
没有 jQueryinnerHtml或其他邪恶/繁重的功能,只有普通的旧 JavaScript:
// Get the first table in the document.
var table = document.getElementsByTagName('table')[0];
// Get the third row of this table (0-index 3rd = 2)
var emailRow = table.rows[2];
// Get this element's content.
var emailContent = emailRow.firstChild.textContent;
You could write it in 1 line:
你可以把它写成 1 行:
var emailContent = document.getElementsByTagName('table')[0].rows[2].firstChild.textContent;
If you want to find allemail addresses in a table:
如果要在表中查找所有电子邮件地址:
var emails = [];
var table = document.getElementsByTagName('table')[0];
var rows = table.rows;
for (var i = 0; i < rows.length; i++) {
var rowText = rows[i].firstChild.textContent;
if (~rowText.indexOf('@')) { // If the content of the row contains a '@' character (This could be replaced with a regex check)
// Also, I personally prefer to use '~' over '> -1' for indexOf(), but both would work.
emails.push(rowText);
}
}
console.log(emails);
回答by Ahmad Awais
If like me you want to get the text from all the first column items in all the tables on the page then use this.
如果像我一样,您想从页面上所有表格的所有第一列项目中获取文本,请使用它。
jQuery('table tr td').each( function( cmp ) {
console.log( jQuery(this).text() );
} );
回答by tombeynon
Assuming you're using vanilla Javascript (no libraries such as jQuery), and that this is the only table on the page, you can use the following code to select the third tr in the table, then find out what the td element contains
假设您使用的是 vanilla Javascript(没有 jQuery 等库),并且这是页面上唯一的表格,您可以使用以下代码选择表格中的第三个 tr,然后找出 td 元素包含的内容
var table = document.getElementsByTagName("table")[0];
var emailTr = table.rows[2];
var emailTd = emailTr.cells[0];
var email = emailTd.innerHTML;
jQuery would make this easier
jQuery 会让这更容易
var email = $("table").children("tr:eq(2)").children("td").html();
回答by Akhil Sekharan
A simple way is to give it a common class. Try:
一个简单的方法是给它一个公共类。尝试:
<table>
<tr><td class="email">foo</td></tr>
<tr><td class="email">bar</td></tr>
<tr><td class="email">[email protected]</td></tr>
</table>
<script>
function getEmail(){
var email = new Array();
var arr = document.getElementsByClassName('email');
for(var i=0; i< arr.length; i++){
email.push(arr[i].innerHTML);
}
alert(email.join(','));
}
</script>
回答by Nelson
This is a solution in case you are using or plan to use jQuery library.
如果您正在使用或计划使用jQuery 库,这是一个解决方案。
Given the email is always in the third row and first column (like in your example) then you can do as follows:
鉴于电子邮件始终位于第三行第一列(如您的示例中),那么您可以执行以下操作:
email = $('table tr:nth-child(3) td:first-child').html();
See working demo
查看工作演示
回答by Kevin Brydon
Get all the <tr>elements. Loop through each one and compare the innerHTMLagainst a regex that matches email addresses.
获取所有<tr>元素。循环遍历每个并将其innerHTML与匹配电子邮件地址的正则表达式进行比较。
var emailAddresses = [];
var cells = document.getElementsByTagName("td");
for (var i = 0; i < cells.length; i++) {
if (cells[i].innerHTML.match(/yourEmailRegex/g)) {
emailAddresses[emailAddresses.length] = cells[i].innerHTML;
}
}
Find the appropriate regular expression here http://www.regular-expressions.info/email.html
在这里找到合适的正则表达式http://www.regular-expressions.info/email.html
回答by Renish Gotecha
in my case i want fifth column value of last row
在我的情况下,我想要最后一行的第五列值
var rows = document.getElementsByTagName("tbody")[0].rows;
var last = rows[rows.length - 1];
var cell = last.cells[4];
console.log(cell.textContent);

