jquery的clone事件导致<a>点击事件失效

当使用jquery的append方法添加元素后,发现链接点击失效。

 $('#profile13 form a').click(function() {
        var dv=$(this).parent().parent().clone();
        $('#profile13 form').append(dv);
     });

  于是用on绑定事件的方法替换原来的代码:

$(document).on('click','#profile13 a',function() {
        var hm=$(this).html();
        if(hm=='[+]'){
           var dv=$(this).parent().parent().clone();
           $('#profile13 form').append(dv);
           var hm=$(this).html();
           if(hm=='[+]'){
            $(this).html('[-]');
           }
       }
       else if(hm=='[-]'){
            $(this).parent().parent().remove();
       }
    });

  这样就能完美实现apend添加元素,并且点击事件依然有效。

posted @ 2022-10-08 20:44  哆啦阿梦  阅读(178)  评论(0编辑  收藏  举报