jQuery removeClass不能与Draggable一起使用

莫格伦的大脑

因此,我试图制作一个时间表,可以将讲义从一个单元拖到另一个单元中,除非新的单元已经有一个讲义,在这种情况下,将被拒绝。我正在尝试通过从单元格中添加和删除类“ has_lecture”来表明是否应该允许将单元格移入或移出来进行此操作。这是我的代码..但是由于某种原因,删除类不起作用。

$(".draggable").draggable({
            revert: 'invalid'
        });

        $(".droppable").droppable({
            accept: function () {
                return (!$(this).hasClass('has_lecture'))
            },
            drop: function (event, ui) {
                var $this = $(this);
                ui.draggable.position({
                    my: "center",
                    at: "center",
                    of: $this,
                    using: function (pos) {
                        $(this).animate(pos, 200, "linear");
                    }
                });
                $(this).addClass('has_lecture')

            },
            out: function () {
                    // this line doesn't work
                    $(this).removeClass('has_lecture')
            }
        });
辣椒坚果

每当可拖动对象移动到可放置对象上方时,accept函数就会运行。这意味着它将检查何时拖入,并且还会检查何时拖出。将其放入并设置has_lecture类后,您的accept函数将在尝试将其拖出时运行,并且由于它具有has_lecture该类,因此它始终返回false,并且永远不会给您的out函数运行的机会。

因此,知道了这种意外行为后,您的想法是:如果放置区为空,则只有在没有has_lecture类的情况下才应接受。但是,如果dropzone不为空,则应该继续接受其中的当前元素。这样可以将其拖出。因此,将元素拖到放置区后,通过将其对引用存储在放置区上$(this).data('dropped', ui.draggable),现在您的accept函数可以检查您要拖入拖出的元素是否已被拖入。 。

(添加了HTML和CSS,因此该代码段将运行)

$(function () {
    $(".draggable").draggable({
        // revert: 'invalid'
    });

    $(".droppable").droppable({
        accept: function (draggable) {
            // new code: new accept clause, accept is not has_lecture OR if the element already dragged onto it is the one you are attempting to drag out
            return (!$(this).hasClass('has_lecture') || $(this).data('dropped') && $(this).data('dropped').is(draggable))
        },
        drop: function (event, ui) {
            var $this = $(this); // << your existing code
            ui.draggable.position({ // << your existing code
                my: "center", // << your existing code
                at: "center", // << your existing code
                of: $this, // << your existing code
                using: function (pos) { // << your existing code
                    $(this).animate(pos, 200, "linear"); // << your existing code
                } // << your existing code
            }); // << your existing code

            $this.addClass('has_lecture'); // << your existing code
            // new code: store a reference to the dropped element on the drop zone
            $this.data('dropped', ui.draggable)

        },
        out: function () {
            $(this).removeClass('has_lecture'); // << your existing code
            // new code: once you have moved it out, then delete the reference to it since your drop zone is now empty again
            $(this).data('dropped', null);
        }
    });
});
    .draggable {
        width: 90px;
        height: 90px;
        padding: 0.5em;
        float: left;
        margin: 10px 10px 10px 0;
        border: 1px solid black;
    }

    .droppable {
        width: 120px;
        height: 120px;
        padding: 0.5em;
        float: left;
        margin: 10px;
        border: 1px solid black;
    }

    h3 {
        clear: left;
    }

    .has_lecture {
        background-color: blue;
    }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>

<div class="draggable" class="ui-widget-content">
    <p>Drag me to my target</p>
</div>

<div class="draggable" class="ui-widget-content">
    <p>Drag me to my target</p>
</div>

<div class="draggable" class="ui-widget-content">
    <p>Drag me to my target</p>
</div>

<div class="droppable" class="ui-widget-header">
    <p>Drop here</p>
</div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

JQuery Mobile和Firefox不能一起玩吗?

来自分类Dev

使jQuery与Turbolinks一起使用

来自分类Dev

rails 4,当需要jquery,jquery_ujs时,link_to不能与destroy动作一起使用

来自分类Dev

为什么选择/取消选择复选框只能与jQuery一起使用?

来自分类Dev

jQuery .then()和成功块不能一起使用吗?

来自分类Dev

Magento静态块和jQuery手风琴不能一起使用

来自分类Dev

为什么多个类选择器不能与removeClass一起使用

来自分类Dev

仅JQuery与FormData一起使用?

来自分类Dev

选择的Jquery脚本不能与Asp.net更新面板一起使用?

来自分类Dev

jQuery类选择器不能与动态类选择器一起使用

来自分类Dev

为什么这个Bootstrap复选框不能与jQuery一起使用?

来自分类Dev

jQuery(metis-menu)不能与流星中的iron:router一起使用

来自分类Dev

为什么jQuery Selectable插件不能与foreach生成的列表一起使用?

来自分类Dev

jQuery .load()函数不能与DataTables一起使用?

来自分类Dev

jQuery选择器不能与变量一起使用

来自分类Dev

jQuery preventDefault()无法与.on()一起使用

来自分类Dev

为什么childElementCount不能与jQuery一起使用?

来自分类Dev

jQuery / ajax表单提交不能与多种表单一起使用+模式不能关闭

来自分类Dev

jQuery不能与underscore.js一起使用

来自分类Dev

jQuery .load不能与函数参数一起使用

来自分类Dev

可调整大小的IFrame,但不能与JQuery一起拖动

来自分类Dev

将CSS3 Transitions和Animations与jQuery addClass / removeClass一起使用的最佳方法是什么?

来自分类Dev

不能使AngularJS与jQuery Summernote一起使用,它根本不显示

来自分类Dev

胖箭头功能不能与jQuery getJSON一起使用?

来自分类Dev

jQuery长度不能与名称中具有变量的元素一起使用

来自分类Dev

为什么JQuery不能与CryptoJS一起使用?

来自分类Dev

jQuery选择和mCustomScrollbar不能一起正常工作

来自分类Dev

jQuery不能与Materialize一起使用以隐藏选择选项

来自分类Dev

为什么 jquery isVisible 不能与滚动功能一起使用

Related 相关文章

  1. 1

    JQuery Mobile和Firefox不能一起玩吗?

  2. 2

    使jQuery与Turbolinks一起使用

  3. 3

    rails 4,当需要jquery,jquery_ujs时,link_to不能与destroy动作一起使用

  4. 4

    为什么选择/取消选择复选框只能与jQuery一起使用?

  5. 5

    jQuery .then()和成功块不能一起使用吗?

  6. 6

    Magento静态块和jQuery手风琴不能一起使用

  7. 7

    为什么多个类选择器不能与removeClass一起使用

  8. 8

    仅JQuery与FormData一起使用?

  9. 9

    选择的Jquery脚本不能与Asp.net更新面板一起使用?

  10. 10

    jQuery类选择器不能与动态类选择器一起使用

  11. 11

    为什么这个Bootstrap复选框不能与jQuery一起使用?

  12. 12

    jQuery(metis-menu)不能与流星中的iron:router一起使用

  13. 13

    为什么jQuery Selectable插件不能与foreach生成的列表一起使用?

  14. 14

    jQuery .load()函数不能与DataTables一起使用?

  15. 15

    jQuery选择器不能与变量一起使用

  16. 16

    jQuery preventDefault()无法与.on()一起使用

  17. 17

    为什么childElementCount不能与jQuery一起使用?

  18. 18

    jQuery / ajax表单提交不能与多种表单一起使用+模式不能关闭

  19. 19

    jQuery不能与underscore.js一起使用

  20. 20

    jQuery .load不能与函数参数一起使用

  21. 21

    可调整大小的IFrame,但不能与JQuery一起拖动

  22. 22

    将CSS3 Transitions和Animations与jQuery addClass / removeClass一起使用的最佳方法是什么?

  23. 23

    不能使AngularJS与jQuery Summernote一起使用,它根本不显示

  24. 24

    胖箭头功能不能与jQuery getJSON一起使用?

  25. 25

    jQuery长度不能与名称中具有变量的元素一起使用

  26. 26

    为什么JQuery不能与CryptoJS一起使用?

  27. 27

    jQuery选择和mCustomScrollbar不能一起正常工作

  28. 28

    jQuery不能与Materialize一起使用以隐藏选择选项

  29. 29

    为什么 jquery isVisible 不能与滚动功能一起使用

热门标签

归档