【问题标题】:jQuery append to the closest element as I unhide当我取消隐藏时,jQuery 追加到最近的元素
【发布时间】:2013-10-30 03:54:08
【问题描述】:

您好,我有一个 jQuery 函数,它显示前三个已被勾选的元素并隐藏其余元素。我有一个功能,可以通过单击添加按钮一次隐藏一个复选框。

我想更进一步,最初我有一个静态的删除按钮,但是现在我想让这个“删除按钮”附加 jQuery,因为我只希望这个按钮在用户点击添加时可见删除相应的 tr。

我有要删除的代码只是不附加它似乎将它添加到所有行中,这很明显,因为我提到了一个类。而不是某个特定的地方,但我不确定我应该如何实现这一点。

我们将非常欢迎任何帮助!

我的代码在下面或查看jsfiddle

  $("#add").click(function () {
        $(".contact_numbers:hidden:first").fadeIn("slow");
        $( ".contact_numbers" ).append( "<a href='#' class='remove'>Remove</a>");
    });

【问题讨论】:

    标签: jquery string append


    【解决方案1】:

    DEMO

    使用fadeIn duration complete回调函数

    动画完成后调用的函数

    $("#add").click(function () {
        $(".contact_numbers:hidden:first").fadeIn("slow", function () {
            $(this).closest('.contact_numbers').append("<a href='#' class='remove'>Remove</a>")
        });
    });
    

    参考

    .closest()

    更好的代码

    DEMO

    $("#add").click(function () {
        $(".contact_numbers:hidden:first").fadeIn("slow", function () {
            $(this).closest('.contact_numbers').find('.remove').remove();
            $(this).closest('.contact_numbers').append("<a href='#' class='remove'>Remove</a>")
        });
    });
    

    在 Op 的评论后更新

    DEMO

    $("#add").click(function () {
        $(".contact_numbers:hidden:first").fadeIn("slow", function () {
            $(this).closest('.contact_numbers').find('.remove').remove();
            $(this).closest('.contact_numbers').find('td:last').append("<a href='#' class='remove'>Remove</a>")
        });
    });
    

    【讨论】:

    • 非常感谢!知道如何让它显示在 td input_number 中,这样它就不会出现在表格之外?
    • 如果您看到 jsfiddle 删除链接出现在表格之外,我希望它出现在电话号码旁边,我已将此 td 类命名为 "input_number" ,你明白什么我是说?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-10
    • 2010-11-28
    • 2013-09-19
    • 1970-01-01
    • 2013-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多