jQuery中如何选取table中每行的除最后一个td以外的所有td

如题所述

需要准备的材料分别有:电脑、html编辑器、浏览器。

1、首先,打开html编辑器,新建html文件,例如:index.html,并引入jquery。

2、在index.html中的<script>标签,输入jquery代码:

$('tr').each(function () {

$(this).find('td').each(function (i) {

if (i !== $(this).parent().find('td').length - 1) {

$('body').append($(this).text());

}

});

});

3、浏览器运行index.html页面,此时成功获取了表格除最后一列外的所有单元格的数据并打印了出来。

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-02-27

应该没有一次性,历遍每一行,然后如果是最后一个td不要。

var $tds=$();
$('table tr').each(function(i,o){
    // $t 包含这一行,非最后一个td的集合
    var $t=$(o).find('td:not(:last)');
    $tds=$tds.add($t);
});

// $tds 就是结果

本回答被提问者和网友采纳
第2个回答  2016-03-22
$("table td:not(:last)")
相似回答