[英]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');
});
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.