admin管理员组文章数量:1335171
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.
When I clone the element, it seems my jquery functions won't work anymore on the cloned element (real element still find).
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.
$('.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!
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.
When I clone the element, it seems my jquery functions won't work anymore on the cloned element (real element still find).
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.
$('.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!
Share Improve this question edited Jun 11, 2015 at 20:06 directory asked Jun 11, 2015 at 16:03 directorydirectory 3,1678 gold badges48 silver badges86 bronze badges 1-
1
Use
$element.clone(true, true);
from stackoverflow./questions/9549643/… – lmgonzalves Commented Jun 11, 2015 at 16:04
2 Answers
Reset to default 9You need to use the .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
!
change
$(element).click(handler);
to
$(element).on("click",handler);
本文标签: javascriptjQuery functions not working on cloned elementStack Overflow
版权声明:本文标题:javascript - jQuery functions not working on cloned element - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742251815a2440903.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论