I have elements on the page which are draggable with jQuery. Do these elements have click event which navigates to another page (ordinary links for example).

在允许单击时,最好的方法是什么,以防止单击在删除此类元素时触发,同时允许单击处于非拖放状态?

我对可排序元素有这个问题,但我认为对于一般的拖放有一个解决方案是很好的.

I've solved the problem for myself. After that I found that same solution exists for Scriptaculous, but maybe someone has a better way to achieve that.

推荐答案

Solution is to add click handler that will prevent click to propagate on start of drag. And then remove that handler after drop is performed. The last action should be delayed a bit for click prevention to work.

可排序解决方案:

...
.sortable({
...
        start: function(event, ui) {
            ui.item.bind("click.prevent",
                function(event) { event.preventDefault(); });
        },
        stop: function(event, ui) {
            setTimeout(function(){ui.item.unbind("click.prevent");}, 300);
        }
...
})

可拖动的解决方案:

...
.draggable({
...
        start: function(event, ui) {
            ui.helper.bind("click.prevent",
                function(event) { event.preventDefault(); });
        },
        stop: function(event, ui) {
            setTimeout(function(){ui.helper.unbind("click.prevent");}, 300);
        }
...
})

Jquery相关问答推荐

如何在html css中制作响应表

jQuery:在mousemove事件期间检测按下的鼠标按钮

[本机代码]是什么意思?

jQuery $(document).ready () 触发两次

提交前添加 POST 参数

什么是不使用 jQuery 的经验技术原因?

我如何从 ACE 编辑器中获得价值?

调用 jquery ajax - .fail vs. :error

jQuery 判断 是否存在并且有一个值

未捕获的语法错误:无法在文档上执行querySelector

如何在 jQuery .each() 的每次迭代之间添加暂停?

jQuery $.cookie 不是一个函数

删除索引后的所有项目

jQuery中的Grep与过滤器?

即时替换 css 文件(并将新样式应用于页面)

在 JQuery .trigger 上传递参数

获取没有在css中设置高度的div的高度

jQuery位置href

jquery $(window).width() 和 $(window).height() 在未调整视口大小时返回不同的值

如何在 ReactJS 中使用 JQuery