javascript - jQuery 获取第 n 个 :child from table after creating that with ajax

标签 javascript jquery html

在下面的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/

相关文章:

javascript - 使用 HighChart + JSON 数据设置饼图颜色

javascript - 谷歌翻译API使用js翻译页面

javascript - IE 格式化文本区域中的 HTML

javascript - 告诉 javascript 先运行 jquery ajax

html - 如果其父元素具有特定类,如何覆盖特定 <td> 元素的 CSS 样式

javascript - iron-page 上的 iron-scroll-threshold

jquery - jQuery 中会有类似 jsMath 的库或插件吗?

javascript - 2个jquery绑定(bind)之间的区别

javascript - jquery加载选择一个特定的类

html - 如何让 block 级元素粘在 div 的底部(它本身是绝对定位的)?