如何避免可拖动的jQuery UI也触发click事件 [英] how to avoid jQuery UI draggable from also triggering click event

查看:269
本文介绍了如何避免可拖动的jQuery UI也触发click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

A具有可通过jQuery UI拖动的大div(地图). div有可点击的子div.

A have a large div (a map) that is draggable via jQuery UI draggable. The div has child divs which are clickable.

我的问题是,如果您拖动地图,则在鼠标向上拖动时,会从您开始拖动的任何子div上触发click事件.

My problem is that if you drag the map, on mouse up, the click event is fired on whatever child div you started the drag from.

如果鼠标是拖动的一部分,我该如何阻止鼠标触发click事件(与没有拖动而单击的人相反,在这种情况下,需要单击事件).

How do I stop the mouse up from triggering the click event if its part of a drag (as opposed to someone just clicking without a drag, in which case the click event is desired).

推荐答案

$('.selector').draggable({
    stop: function(event, ui) {
        // event.toElement is the element that was responsible
        // for triggering this event. The handle, in case of a draggable.
        $( event.originalEvent.target ).one('click', function(e){ e.stopImmediatePropagation(); } );
    }
});

之所以可行,是因为在之前正常" 侦听器被触发了一个侦听器" .因此,如果一个监听器停止传播,它将永远无法到达您先前设置的监听器.

This works because "one-listeners" are fired before "normal" listeners. So if a one-listener stops propagation, it will never reach your previously set listeners.

这篇关于如何避免可拖动的jQuery UI也触发click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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