使用 jQuery 选择第三个 <td>?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14957398/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 14:08:54  来源:igfitidea点击:

Select the 3rd <td> using jQuery?

jquery

提问by user1856596

I have a table a row and multiple tds in that row, like that:

我有一个表格一行,该行中有多个 tds,如下所示:

<tr class="first odd">
    <td class="a-center">
    <td>...</td>
    <td>...</td> --> I want this
    <td>...</td> 
    <td>...</td>
    <td>...</td>
</tr>

I am able to get the parent tr of all the tds, but is there a way to get the 3rd td inside the tr only?

我能够获得所有 tds 的父 tr,但是有没有办法只在 tr 中获得第三个 td?

The code to get the tr is:

获取 tr 的代码是:

jQuery('#shopping-cart-table >  tbody > tr').each(function(index, value) {
    ...
});

Thanks

谢谢

回答by undefined

You can use :eqselector:

您可以使用:eq选择器:

jQuery('#shopping-cart-table > tbody > tr').each(function(index, value) {
    $('td:eq(2)', this)
});

Or:

或者:

jQuery('#shopping-cart-table tbody').find('td:eq(2)');

回答by Chris Dixon

Use the :eq function in jQuery: http://api.jquery.com/eq/

在 jQuery 中使用 :eq 函数:http: //api.jquery.com/eq/

$('#shopping-cart-table td').eq(2);

回答by Karthik Chintala

Try like this:

像这样尝试:

$(".first td:nth-child(3)").append("<span>-3rd");

回答by Gautam3164

Try with this

试试这个

$("#shopping-cart-table >  tbody > tr td").eq(2);

回答by Tarun Kumar

this is the best and easiest code . You can click on the button in that row then it will show you the thrid value . if this your table :

这是最好和最简单的代码。您可以单击该行中的按钮,然后它将显示第三个值。如果这是你的桌子:

<tr class="first odd">
    <td class="a-center">
    <td>...</td>
    <td>...</td> --> I want this
    <td>...</td> 
    <td>...</td>
    <td><button id='test'><button></td>
</tr>

then in javascript bind the button with click event like given below:-

然后在 javascript 中使用单击事件绑定按钮,如下所示:-

$(document).ready(function () {
    $('#test').click(function(){
         var thirdData  = $(this).closest('tr').children('td:eq(2)').text();
         alert(thirdData);
    });
});

this will solve the problem .

这将解决问题。

回答by Nalan Madheswaran

Answer for the title:

标题答案:

$("#tableId tr td:nth-child(3)").html();

回答by JD Electromagnet

Sample code, can modify too here

示例代码,这里也可以修改

I think this will solve the problem as you will be able to load any given <td>by changing the last Index of this equation td = tr[i].getElementsByTagName("td")[1];

我认为这将解决问题,因为您可以<td>通过更改此等式的最后一个索引来加载任何给定的内容td = tr[i].getElementsByTagName("td")[1];