开发者

How to clone cloned DOM objects in jQuery

开发者 https://www.devze.com 2023-03-16 18:13 出处:网络
I have <div class=\"OptionRow\">s followed by a <a class=\"AddGroup\"> Inside the开发者_Go百科 .OptionRows, I have an X to remove that row.

I have <div class="OptionRow">s followed by a <a class="AddGroup">

Inside the开发者_Go百科 .OptionRows, I have an X to remove that row.

The .AddGroup works fine until I X the original element that was cloned. Here's my code for the cloning and the X

$('.AddGroup').click(function(e) {
    e.preventDefault();
    var $this = $(this);
    $this.parent().siblings('.OptionRow:first').clone(true, true).hide().insertBefore($this).fadeIn();
});
$('.CloseGroup').click(function(e) {
    e.preventDefault();
    $(this).parents('.OptionRow').fadeOut('fast', function() {
        $(this).remove();
    });
});


The clone wont have the handlers that the original had.

Try using .live('click', function(e){...}) for your click handlers which might fix the issue


As per @Microprocessor's suggestion, I examined the HTML and realized the .OptionRow clone was being inserted into the wrong place in the DOM. I solved the problem by using .insertBefore($this.parent()) instead of .insertBefore($this). Thanks @Microprocessor, I don't know why I didn't do that earlier.

0

精彩评论

暂无评论...
验证码 换一张
取 消