Javascript - get all table -> tr values

前端 未结 8 1691
鱼传尺愫
鱼传尺愫 2020-12-28 23:02
相关标签:
8条回答
  • 2020-12-28 23:20

    This is a solution in case you are using or plan to use jQuery library.

    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

    0 讨论(0)
  • 2020-12-28 23:21

    No jQuery, innerHtml or other evil / heavy functions, just plain old 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:

    var emailContent = document.getElementsByTagName('table')[0].rows[2].firstChild.textContent;
    

    If you want to find all email 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);
    

    Working example

    0 讨论(0)
  • 2020-12-28 23:27

    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() ); 
    } );
    
    0 讨论(0)
  • 2020-12-28 23:31

    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">abc@yahoo.com</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>
    

    Demo

    0 讨论(0)
  • 2020-12-28 23:35

    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);
    
    0 讨论(0)
  • 2020-12-28 23:36

    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

    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

    var email = $("table").children("tr:eq(2)").children("td").html();
    
    0 讨论(0)
提交回复
热议问题
foo
bar
abc@yahoo.com