简体   繁体   English

jQueryUI禁用/取消图标被拖动到可排序列表中吗?

[英]jQueryUI disable / cancel icon from being dragged in a sortable list?

I am having difficulty trying to disable the list elements from being dragged by the icon. 我在尝试禁止列表元素被图标拖动时遇到困难。

Each li has an icon, when clicked either removes or adds it into another list. 每个li都有一个图标,单击可将其删除或添加到另一个列表中。 However if you drag by its icon it removes or adds the item. 但是,如果拖动其图标,它将删除或添加该项目。

I want to be able to drag the li around, but stop the icons from being dragable on the li's. 我希望能够拖拉li,但是要阻止图标在li上拖动。

I have a demo: http://jsfiddle.net/w3vvL/27/ 我有一个演示: http : //jsfiddle.net/w3vvL/27/

I have tried a number of things with no success and I cant figure it out: - 我尝试了很多方法都没有成功,但我无法弄清楚:-

    $( "#gallery li" ).draggable({
          cancel: "a.ui-icon",
          revert: "invalid",
          containment: "document",
          helper: "clone",
          cursor: "move"
        });
    $( ".ui-icon" ).disableSelection();

    //Get Dropped Item
    $("#trash").droppable({
         drop: function(event, ui) {
    $(ui.draggable).stop();

               }
    });

Any help would be greatly appreciated! 任何帮助将不胜感激!

In your jsfiddle, I added the following: 在您的jsfiddle中,我添加了以下内容:

$('.delete, .add').on('mousedown', function(e) {
    e.stopPropagation();
});

This stops the mousedown event from bubbling up, so it inhibits the draggable behaviour, when the icon (an a element inside the li ) is clicked. 这将停止向上冒泡鼠标按下事件,所以它抑制了可拖动的行为,所述图标(一个当a内部元件li被点击)。

HOWEVER, this is not a good solution! 但是,这不是一个好的解决方案! It adds event handlers to the a elements themselves, so it only adds handlers to the elements which exist when you call on . 它增加了事件处理程序的a元素本身,所以它只会增加处理程序,当你调用它存在的元素on If you add a new item (I used the ADD button, luckily it was implemented), the fix doesn't apply . 如果您添加一个新项目(幸运的是,我使用了ADD按钮,则已实现),此修复方法不适用 You would have to add the handler to any new items that are added. 您必须将处理程序添加到添加的任何新项目中。


We could do it a better way. 我们可以做一个更好的方法。 We could add a handler to the containers instead: 我们可以向容器添加处理程序:

$('#gallery, #trash').on('mousedown', '.ui-icon', function(e) {
    e.stopPropagation();
});

The second argument of on lets us specify a filter. on的第二个参数让我们指定一个过滤器。 The #gallery and #trash elements can receive mousedown events bubbling up from child elements, but we only want this handler to execute if the event originated from a .ui-icon . #gallery#trash元素可以接收从子元素#trash mousedown事件,但是我们仅希望该处理程序在事件源自.ui-icon This is pretty good, we only have to set up event handlers once and there's only one per sortable container, so it's easier and more efficient. 这非常好,我们只需设置一次事件处理程序,并且每个可排序的容器只有一个,因此它更简单,更有效。


I decided to check the API documentation , and found that there's a specific option to deal with this: 我决定查看API文档 ,并发现有一个特定的选项可以解决此问题:

$("#gallery, #trash").sortable("option", "cancel", ".ui-icon");

which very cleanly tells the sortable widget not to allow sorting if it's started from a .ui-icon ! 如果它是从.ui-icon开始的,它很清楚地告诉sortable小部件不允许排序! Instead of calling sortable("option") explicitly, you could add this option to the initialization of your sortables. 除了可以显式调用sortable(“ option”)之外,您可以将此选项添加到您的sortables的初始化中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM