简体   繁体   English

如何使用jquery遍历表以选择每一行上的特定表单元格?

[英]How do loop through a table to select a specific table cell on each row using jquery?

I am trying to place text in the 6th table cell of each row of my table. 我试图将文本放置在表格每行的第六个表格单元格中。 But all I am getting is the first row selected: 但是我得到的只是选择的第一行:

$('tbody tr:even td:eq(5)').each(function(){
                $(this).text('$145');
            });

What adjustment do I need to make? 我需要进行哪些调整?

I think that the following should work: 我认为以下应该起作用:

$('tbody tr').each(
function(){
    $(this).find('td:eq(5)').text('$145');
});

JS Fiddle demo . JS小提琴演示

Reference: 参考:

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

  $(this).find( 'td' ).eq(5).text('$145');

});

UPDATE 更新

Since the accepted anwser does the same thing but using the :eq() selector instead of the .eq() method, it's worth reading the additional notes on the jQuery DOCs for the eq selector : 由于接受的anwser会执行相同的操作,只是使用:eq()选择器而不是.eq()方法,因此值得阅读有关eq选择器的jQuery DOC上的其他说明:

Because :eq() is a jQuery extension and not part of the CSS specification, queries using :eq() cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. 因为:eq()是jQuery扩展,而不是CSS规范的一部分,所以使用:eq()的查询无法利用本机DOM querySelectorAll()方法提供的性能提升。 For better performance in modern browsers, use $("your-pure-css-selector").eq(index) instead. 为了在现代浏览器中获得更好的性能,请改用$(“ your-pure-css-selector”)。eq(index)。

So I think it's advisable to use the .eq() method instead of the :eq() selector. 因此,我建议使用.eq()方法而不是:eq()选择器。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用jQuery遍历表格,使用选中的复选框连续更新特定单元格 - Loop through table using jQuery, update specific cell in a row with a checked Checkbox jQuery:如何选择表中除每行的最后一个单元格外的每个单元格? - jQuery: how to select every cell in a table except the last in each row? jquery如何选择每个表行的第一个单元格 - jquery How to select the 1st cell of each table row 如何使用jquery遍历php回显的表行并选择满足属性的特定行 - How to use jquery to loop through table rows echoed by php and select a specific row where a property is meet 如何使用jQuery按单元格内容选择表行 - How to select table row by cell content using jquery JQuery:如何在表的每一行中选择第n个输入? - JQuery: How do I select the nth inputs in each row of a table? 如何使用jQuery从表行中的特定单元格中获取复选框值? - How do I get the checkbox value from a specific cell in a table row using jQuery? 如何遍历表行,更改类,并使用“n”行执行所有操作并了解每行中FIRST单元格的值 - How to loop through table rows, change class, and do it all with “n” number of rows and knowing the values of the FIRST cell in each row 如何使用jquery遍历表中每一行的隐藏字段值 - how do iterate through the hidden field value for each row in a table using jquery 使用jQuery遍历表格-计算每一行的单元格 - Looping through table with jQuery - calculating cell in for each row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM