for (var i = 0; i <= $('#table-body tr').length - 1; i++) {
var ingredients = $('#recipiestable tbody tr:nth-child(2) td:nth-child(1)').text()
var amount = $('#recipiestable tbody tr:nth-child(2) td:nth-child(2)').text()
var unit = $('#recipiestable tbody tr:nth-child(2) td:nth-child(3)').text()
$('#resultsbody').append('<tr><td>' + ingredients + '</td><td>' + amount + '</td><td>' + unit + '</td> </tr>')
}
如何在循环中使用i选择第n个孩子?我试过了:
$('#recipiestable tbody tr:nth-child('+ i +') td:nth-child(1)')
但它只是返回错误,有没有人知道如何解决这个问题?
答案 0 :(得分:3)
如何进行一些重构以更好地理解您的代码?
var table_size = $('#table-body tr').length;
var trs = $('#recipiestable tbody tr').slice(table_size);
$.each(trs, function(i, tr) {
// No need for messy lookups, if you can look inside the context of your current tr
var ingredients = $('td:nth-child(1)', tr).text(),
amount = $('td:nth-child(2)', tr).text(),
unit = $('td:nth-child(3)', tr).text();
$('#resultsbody').append('<tr><td>' + ingredients + '</td><td>' + amount + '</td><td>' + unit + '</td> </tr>');
});
的文档
答案 1 :(得分:0)
for (var i = 0; i <= $('#table-body tr').length - 1; i++) {
var ingredients = $('#recipiestable tbody tr:nth-child(2) td:nth-child(1)').text();
var amount = $('#recipiestable tbody tr:nth-child(2) td:nth-child(2)').text();
var unit = $('#recipiestable tbody tr:nth-child(2) td:nth-child(3)').text();
$('#resultsbody').append('<tr><td>' + ingredients + '</td><td>' + amount + '</td><td>' + unit + '</td> </tr>');
}
请添加分号;