Javascript - get all table -> tr values

前端 未结 8 1692
鱼传尺愫
鱼传尺愫 2020-12-28 23:02
相关标签:
8条回答
  • 2020-12-28 23:39
    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/.

    Obviously you'll have to change document.getElementsByTagName("table")[0] to appropriately match your table

    If you're using jQuery it's easier:

    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.

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

    Get all the <tr> elements. Loop through each one and compare the innerHTML against a regex that matches email addresses.

    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

    0 讨论(0)
提交回复
热议问题
foo
bar
abc@yahoo.com