[英]jQuery dynamically call event listeners
I have a big Problem: I have many Div Containers on my Page with the class ".element" which I make draggable using jQuery. 我有一个大问题:我的页面上有很多Div容器,它们的类为“ .element”,我可以使用jQuery将其拖动。 Now I have the possibility to dynamically create divs with the class "element", and I want to make them draggable as well.
现在,我可以使用类“ element”动态创建div了,我也希望使其可拖动。 So I thought: Simple solution: I just call
所以我想:简单的解决方案:我只是打电话给
$(".element").draggable(); $( “元件”)可拖动();
again. 再次。 But: The whole listener is executed twice then, which unacceptable for me because it sends AJAX-Requests and i don't want them to be sent twice.
但是:整个侦听器随后执行两次,这对我来说是不可接受的,因为它发送了AJAX请求,而我不希望它们发送两次。
So I started to make Functions to call draggable only on the newly created element, which is kind of stupid I guess. 因此,我开始使Functions只能在新创建的元素上调用draggable,我认为这是一种愚蠢的做法。
So my question: Is there any better possibility since the live()-Method is deprecated (which was a big mistake in my opinion!) 所以我的问题是:既然live()方法已被弃用,还有什么更好的可能性(我认为这是一个大错误!)
Please help me! 请帮我!
Kind regards, David 亲切的问候,大卫
use event.stopPropagation()
使用
event.stopPropagation()
in your event..it will stop event to trigger twice.... 在您的事件中..它将停止事件触发两次。
i hope it work for you... 我希望它对您有用...
A fast solution could be to use a data-attr
as flag 一种快速的解决方案是将
data-attr
用作标志
$(".element:not(data-draggable='1')")
.draggable()
.attr("data-draggable","1");
Note: live
method is deprecated but you can use on
. 注意:不建议使用
live
方法,但可以on
使用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.