jQuery如何在可拖动对象上使用.live [英] Jquery How to use .live on draggable

查看:77
本文介绍了jQuery如何在可拖动对象上使用.live的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用来自jQuery UI的dragabble方法.如何在可拖动对象上应用live().

I am using dragabble method from jquery ui. How to apply live() on draggable.

$("#image").draggable({ containment: [10, 150, 0, 0], scroll: false});

我尝试过的是这个

$("#image").live("draggable", function () {
.draggable({ containment: [10, 150, 0, 0], scroll: false});

但这不起作用.

谢谢

推荐答案

首先,仅供参考,不建议使用live,您应该使用.on()作为以上状态的注释.

Firstly as an FYI, live is deprecated, you should be using .on() as the comments above state.

第二,您将无法在两种情况下都需要做的事情,因为这些事件没有被放入on()中.因此,我要采用的方法是在函数内执行事件附加:

Secondly, you won't be able to do what you need to do with either scenario as those events aren't baked into on(). Therefore the way that I would approach it is to perform your event attachment inside a function:

function doDraggable() {
    $(".draggable").draggable({ containment: [0, finalHeight, 0, 0], scroll: false});
}

然后在文档准备就绪以及ajax完成时对其进行初始化:

Then initialise it when the document is ready and also whenever ajax completes:

$(document).ready(function () {
    doDraggable();
});
$(document).ajaxComplete(function () {
    doDraggable();
});

使用ajaxComplete事件,您可以比文档选择器更具体,这样它就不会在每一个 ajax事件中触发,但是您会感到很困惑...

You can be more specific than the document selector using the ajaxComplete event so that it doesn't fire for every ajax event, but you get my drift...

这篇关于jQuery如何在可拖动对象上使用.live的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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