【问题标题】:jQuery removeClass not working with draggablejQuery removeClass 不适用于可拖动
【发布时间】:2021-01-17 08:47:39
【问题描述】:

所以我正在尝试制作一个日程表,可以将讲座从一个单元格拖动到另一个单元格,除非新单元格已经有一个讲座,在这种情况下拖动将被拒绝。我试图通过在单元格中添加和删除类“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')
            }
        });

【问题讨论】:

  • 您看到任何错误吗?可以分享下这段代码对应的html sn-p吗?

标签: javascript jquery jquery-ui-draggable


【解决方案1】:

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

所以,知道这种意外行为,想法是:如果放置区是空的,它应该只接受没有 has_lecture 类。但如果 dropzone 不为空,它应该继续接受当前在其中的元素。这将允许您将其拖出。因此,一旦您将元素拖入放置区,通过$(this).data('dropped', ui.draggable) 存储对放置区的元素的引用,现在您的接受函数可以检查您尝试拖动的元素是否 out是已经被拖进去的那个。

(添加了 HTML 和 css 以便 sn-p 运行)

$(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>

【讨论】:

  • 谢谢,它成功了,但你知道如果可拖动项目最初附加在 droppable 中,我该如何启动 has_lecture 类吗?一旦动态附加可拖动对象,我尝试 1- 添加 has_lecture 类,但它被视为原始类。我的意思是当我将可拖动的单元格从 a 移动到 b 时,has_lecture 类不会被删除,但从 b 到 c 它会被删除 2-我尝试 create: function (event, ui){ } 但它也没有用。
  • 嘿@HeilaAl-Mogren,我认为问题在于,如果您要以编程方式将元素预先附加到可放置对象,则必须使用jQuery.simulate 插件;如果您只是使用 append 将元素附加到 dropzone 或者它不会工作。看到我制作的这个小提琴:jsfiddle.net/nLt0q5h2 并看到另一个关于动态附加到 droppable 的 stackoverflow:stackoverflow.com/questions/16848165/… 和这个 codepen 我找到了trigger_drop 函数codepen.io/xlicodes/pen/ZMLmxZ
猜你喜欢
  • 2012-01-15
  • 2013-01-17
  • 2012-11-07
  • 2013-11-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
相关资源
最近更新 更多