我正在尝试将文本放置在表格每一行的第 6 个表格单元格中。但我得到的只是选择的第一行:
$('tbody tr:even td:eq(5)').each(function(){
$(this).text('$145');
});
我需要做哪些调整?
我正在尝试将文本放置在表格每一行的第 6 个表格单元格中。但我得到的只是选择的第一行:
$('tbody tr:even td:eq(5)').each(function(){
$(this).text('$145');
});
我需要做哪些调整?
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:
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. For better performance in modern browsers, use $("your-pure-css-selector").eq(index) instead.
So I think it's advisable to use the .eq()
method instead of the :eq()
selector.