jquery 如何从表中的第一列获取文本?

to94eoyn  于 8个月前  发布在  jQuery
关注(0)|答案(6)|浏览(86)

在网页中有一个表(在我的例子中是http://developer.chrome.com/extensions/api_index),我想获取Stable API中的所有方法名称。所以我想获取一个数组,其元素是第一列中的内容。
怎么做?

$("table:eq(5) tr td:eq(0)")

字符串
这段代码不起作用,因为它没有从所有行中的所有第一个td元素中获取文本,而只是从一行中获取文本。怎么办?

sshcrbum

sshcrbum1#

你可以尝试

$(table).find('td:first').text() //can you table id or class

字符串
可以对所有行执行循环以获取第一个单元格值

var array = new Array();

$('table tr').each(function () {
    var firstCell = $(this).find('td').first();
    array.push($firstCell.text());
});

omtl5h9j

omtl5h9j2#

你可以使用:first-child选择器(http://www.w3schools.com/cssref/sel_firstchild.asp),它将为每一行选择第一个表格单元格。

rm5edbpk

rm5edbpk3#

var array = [];

jQuery('table tr').each(function () {
    var $row = $(this);
    var $firstCell = $row.find('td:first');
    array.push($firstCell.text());
});

字符串

kxeu7u2r

kxeu7u2r4#

试试看:

var a = new Array();

$("table:eq(5) tr td:first-child").each(
    function(){
        a.push($(this).text())
    });

字符串

ztyzrc3y

ztyzrc3y5#

您可以像这样遍历所有表行:

$("#tableId > tbody > tr").each(function(){
    var name = $(this).find("td:first").text();
    console.log("Method name: "+name);
});

字符串
你可以全局初始化一个数组并在循环中保存该数组。希望这对你有帮助。

hsgswve4

hsgswve46#

这给出了第一个td的选择器。

$('#package_list_table tr td:first-child')

字符串

相关问题