jQuery draggable - 如果对元素应用两次会发生什么? [英] jQuery draggable - what happens if it is applied twice to an element?

查看:94
本文介绍了jQuery draggable - 如果对元素应用两次会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到最近我才设置了这个设置,多次调用:

Until recently I had this set up, which was called multiple times:

                $('.rsh')
                    .draggable('destroy')                               
                    .draggable({ blah blah details });

destroy 是否可以停止多个可拖动处理程序在课堂上积累。 AJAX正在创建新元素,并且 draggable 的初始附件不会触及随后创建的元素。

The destroy was there to stop multiple draggable handlers accumulating on the class. New elements are being created by AJAX, and an initial attachment of draggable to the class doesn't touch subsequently created elements.

然而,当我更新到jQuery UI的1.9.2版时,它开始给我这个错误:

However, when I updated to version 1.9.2 of jQuery UI, it started giving me this error:


错误:无法调用初始化之前可拖动的方法;试图调用方法'destroy'

Error: cannot call methods on draggable prior to initialization; attempted to call method 'destroy'

所以我删除了破坏线,它很甜。除了......我怀疑我现在可能会在课堂上添加越来越多的处理程序(这就是为什么 destroy 首先出现的原因)。

So I removed the destroy line, and it's sweet. Except... I suspect that I may now be adding more and more handlers to the class (which is why the destroy was there in the first place).

我试过这个,但它不喜欢它:

I tried this, but it didn't like it:

if ($('.rsh').length) {
    $('.rsh').draggable('destroy'); 
}

两个问题:(1)是否会有越来越多的处理程序附加到每次我开火可拖动的设置线? (2)如果是这样,有关如何删除它们的任何解决方案?

Two questions: (1) Will there be more and more handlers attached to the class each time I fire the draggable set up line? (2) If so, any solutions on how to remove them?

推荐答案

不,不会有额外的处理程序绑定。 jQuery将初始化的实例注册到元素,并且不会为同一元素创建相同小部件的新实例。

No, there won't be extra handlers bound. jQuery registers the initialized instance to the element and won't create a new instance of the same widget for the same element.

因为你担心处理程序,所以这是一个快速检查(jQuery 1.8+和UI 1.9 +):

As you're worried about handlers, here's a quick check (jQuery 1.8+ and UI 1.9+):

$('div').draggable();
console.log( $._data($('div')[0], 'events') );
$('div').draggable();
console.log( $._data($('div')[0], 'events') );

小提琴

如您所见,尝试初始化新的可拖动实例后,附加的处理程序对象不会更改在同一元素上。

As you can see, the attached handlers object is not altered after trying to initialize a new draggable instance on the same element.

编辑:虽然不会忽略带参数的后续调用,而是扩展现有的小部件,如图所示。 @Jason Sperske的回答。

edit: Subsequent calls with parameters won't be ignored though, rather they will extend the existing widget as shown on @Jason Sperske's answer.

这篇关于jQuery draggable - 如果对元素应用两次会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆