在下面的ajax中,我在获取数据后填充并创建了简单的数据
$.ajax({
method: 'GET',
url: '/analyzePage/searchTag/' + tagName,
contentType: false,
processData: false,
success: function (data) {
console.log(data);
setTimeout(function () {
if (data.state == 'error') {
...
}
else {
let table = '<div class="table-responsive pre-scrollable">';
...
$.each(data.tags, function (key, value) {
table = table + '<tr class="searchTag-' + value.name + '">';
table = table + '<td style="padding: 6px 20px;">1</td>';
table = table + '<td style="padding: 6px 0px;">';
table = table + '<img src="' + value.profile_pic_url + '" class="img-circle img-sm" id="tagIamge"/></td>';
table = table + '<td style="padding: 6px 20px;">' + value.name + '</td>';
table = table + '<td style="padding: 6px 20px;">' + value.media_count + '</td>';
table = table + '<td>';
table = table + '<button type="button" class="btn btn-warning btn-icon btn-rounded legitRipple" id="searchTag-' + value.name + '">';
table = table + '<i class="icon-file-plus"></i>';
table = table + '</button>';
table = table + '</td>';
table = table + '</tr>';
});
...
$('.data').html(table);
}
}, 800);
},
error: function (data) {
console.log(data);
}
});
因为我有一个按钮 id="searchTag-' + value.name + '"
它是最新的 td
我想得到这个按钮的所有列就是那个,例如这个 tr
有 4 个 td
和最新的 td
我有这个按钮,然后我想得到其他 点击按钮后 td
进入此 tr
我的代码可以工作,但它会将所有 tr
放入我的表中
$(document).on('click', "[id^='searchTag']", function () {
let thisId = $(this).attr("id");
let tagName = thisId.split("-")[1];
$("[class^='" + $(this).attr("id") + "'] td:nth-child(3)").append("<span>Clicked</span>");
});
最佳答案
点击后要在按钮图标旁边添加一个跨度吗?
如果你这样做,你可以这样做:
$(document).on('click', "[id^='searchTag']", function () {
$(this).closest('td').append("<span>Clicked</span>");
});
i want to get all td which button is into this row
试试这个:
$(document).on('click', "[id^='searchTag']", function () {
$(this).closest('tr').find('td'); // returns all "td" elements
});
关于javascript - jQuery 获取第 n 个 :child from table after creating that with ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50892269/