获取所选数据表tr的第二个td的html

时间:2015-01-14 11:54:49

标签: javascript jquery html

我有一个数据表...

现在我有了这个代码,当单击时,它会将类或tr转换为“选中”..

$(document).ready(function()
{
    $("#domains_list tbody").on( "click", "tr", function () 
        {
            $(this).toggleClass("selected");
        } );

});

现在我的数据表就像这样......

enter image description here

enter image description here

现在您可以看到有两个页面显示不同的选定行

现在我写这段代码

$('#butto').click(function()
{
$("#domains_list").find("tr.selected").each(function() 
    {
        $(this).find("td:eq(1)").each(function()
            {
                alert($(this).html());
            });
    });
});

以上代码旨在显示所选htm

的第二个<td>中的所有<tr> l

但问题是,它仅提醒<td><tr>的{​​{1}} html ...

即。 opened page

if page 1 is open then it alerts only <td> html of page 1,

我怎样才能提醒所有html,无论页面打开了什么?

2 个答案:

答案 0 :(得分:0)

对于&#34;第二个td&#34;,使用nth-of-type

$(this).find("td:nth-of-type(2)").each(function() {
    alert($(this).html());
});

您可以像这样使用它......

$("#domains_list").find("tr.selected td:nth-of-type(2)").each(function() {
    alert($(this).html());
});

:nth-​​of-type(n)选择器匹配其父元素的特定类型的第n个子元素。

提示:查看:nth-​​child()选择器,选择其父元素的第n个子元素,无论其类型如何。

答案 1 :(得分:0)

使用您的示例页面,我能够使用以下代码记录每个选定行的第一个TD内的所有名称:

$("tr.selected").each(function(){
console.log($(this).find("td:first").html());
});

如果你需要的TD不是第一个,你可以使用:nth-child(index)选择器代替:first

检查以下图片,如果这不是您想要的,请告诉我。

enter image description here

相关问题