Getting text from td cells with jQuery

后端 未结 5 1483
有刺的猬
有刺的猬 2020-12-11 00:00

I have this code in jQuery:

children(\'table\').children(\'tbody\').children(\'tr\').children(\'td\')

Which gets all table cells for each r

相关标签:
5条回答
  • 2020-12-11 00:41

    I would give your tds a specific class, e.g. data-cell, and then use something like this:

    $("td.data-cell").each(function () {
        // 'this' is now the raw td DOM element
        var txt = $(this).html();
    });
    
    0 讨论(0)
  • 2020-12-11 00:50
    $(document).ready(function() {
      $('td').on('click', function() {
        var value = $this.text();
      });
    });
    
    0 讨论(0)
  • 2020-12-11 00:51

    First of all, your selector is overkill. I suggest using a class or ID selector like my example below. Once you've corrected your selector, simply use jQuery's .each() to iterate through the collection:

    ID Selector:

    $('#mytable td').each(function() {
        var cellText = $(this).html();    
    });
    

    Class Selector:

    $('.myTableClass td').each(function() {
        var cellText = $(this).html();    
    });
    

    Additional Information:

    Take a look at jQuery's selector docs.

    0 讨论(0)
  • 2020-12-11 01:07

    You can use .map: http://jsfiddle.net/9ndcL/1/.

    // array of text of each td
    
    var texts = $("td").map(function() {
        return $(this).text();
    });
    
    0 讨论(0)
  • 2020-12-11 01:07
    $(".field-group_name").each(function() {
            console.log($(this).text());
        });
    
    0 讨论(0)
提交回复
热议问题