简体   繁体   English

将元素追加到另一个元素后的jQuery双击事件

[英]Jquery double click event after appending element to another element

I'm using agile toolkit, a framework that generates javascript code from PHP. 我正在使用敏捷工具包,这是一个从PHP生成javascript代码的框架。 I have a div element (I'll call it "top-element") that contains some other div elements, some buttons. 我有一个div元素(我称其为“顶部元素”),其中包含一些其他div元素,一些按钮。 I want to move the "top-element" to another element, to change it's parent. 我想将“顶部元素”移动到另一个元素,以更改其父元素。

I tried something like: 我尝试了类似的东西:

$('#top-element').appendTo($('#new-parent'));

But the problem is that the "top-element" have some childrens that have click events, some buttons. 但是问题是“顶部元素”有一些带有点击事件的孩子,一些按钮。 After I append the "top-element" to a new element (after changing it's parent), the click events are triggered twice. 在将“ top-element”附加到新元素(更改其父元素)后,click事件被触发两次。

I tried to clone the element and append the cloned element to the new parent: 我尝试克隆元素并将克隆的元素附加到新的父元素:

var cloned_top_element = $('#top-element').clone(true);
cloned_top_element.appendTo($('#new-parent'));

I got the same problem, the click event on "top-element" childrens was called twice. 我遇到了同样的问题,“顶级元素”儿童的click事件被调用了两次。

The way to prevent double click is to use: 防止双击的方法是使用:

unbind('click') or off('click')

I tried something like: 我尝试了类似的东西:

$('#new-parent').find('.children-class').unbind('dblclick').unbind('click');

But still no results. 但是仍然没有结果。

The binding for child buttons is like this: 子按钮的绑定是这样的:

$('.children-class').bind('click',function(ev){ ev.preventDefault();ev.stopPropagation(); other stuff });

The bind function appears only once. 绑定功能仅出现一次。 There aren't duplicates in the js code. js代码中没有重复项。

Any ideas? 有任何想法吗? Anticipated thanks. 期待中的感谢。

Remove the true in the clone function .clone(); 克隆函数.clone();删除true .clone(); this will not copy the event handlers 这不会复制事件处理程序

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

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