简体   繁体   English

jQuery函数不适用于克隆元素

[英]jQuery functions not working on cloned element

I've an element which I clone, there are some simple jQuery events/functions on them like a click action (I've set a log.console in this function) to do some small actions. 我克隆了一个元素,上面有一些简单的jQuery事件/函数,例如单击动作(我在此函数中设置了log.console)来执行一些小动作。

When I clone the element, it seems my jquery functions won't work anymore on the cloned element (real element still find). 当我克隆元素时,似乎我的jquery函数在克隆的元素上不再起作用了(仍然可以找到真实元素)。

Is there an reason for the behavior, and how can I solve this? 是否有这种行为的原因,我该如何解决?

(update) (更新)

My clone, and my remove button. 我的克隆和删除按钮。 I've added true in the clone function but still nothing is happening. 我在克隆函数中添加了true,但是仍然没有任何反应。

    $('.clone-row').click(function() {

        var row = $(this).prev().prev();
        $(row).clone(true, true).append('<span class="remove">remove</span>').hide().appendTo('.clones').css('opacity', 0).slideDown(350).animate({ opacity: 1 },{ queue: false, duration: 'slow' });

    });
    // clone works fine..

    $('.remove').click(function(){

        console.log('remove');

    });
    // nothing happens

Many thanks! 非常感谢!

You need to use the .clone( [withDataAndEvents ] [, deepWithDataAndEvents ] ) : 您需要使用.clone( [withDataAndEvents ] [, deepWithDataAndEvents ] )

$.clone( true, true )

A Boolean indicating whether event handlers and data should be copied along with the elements. 一个布尔值,指示是否应将事件处理程序和数据与元素一起复制。

By default, this is false ! 默认情况下,这是false

change 更改

 $(element).click(handler);

to

 $(element).on("click",handler);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM