【问题标题】:Allow element to drag & drop in one TR only只允许元素在一个 TR 中拖放
【发布时间】:2014-12-23 06:28:15
【问题描述】:

在这个jsFiddle。如果元素的 TR 发生更改,我想添加防止拖放功能。

元素只能在自己的 TR 中拖放。我该怎么做?

我应该在 accept 方法中添加一些限制吗?

以下是JS代码:

$(function() {
                $(".dragDiv").draggable({
                    revert: 'invalid'
                });
                $(".mainTable tr td").droppable({
                    accept: function() {
                        var $this = $(this);
                    console.log(JSON.stringify($this.data("parent-id")+'==='+$this.parent().attr("id")));
                    if (($this.data("parent-id") == $this.parent().attr("id")) && $(this).find("*").length == 0)
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                    },
                    drop: function(event, ui) {

                        var $this = $(this);
                        $this.append(ui.draggable.css({
                            top: 0,
                            left: 0
                        }));
                        ui.draggable.position({
                            my: "center",
                            at: "center",
                            of: $this,
                            using: function(pos) {
                                $(this).animate(pos, "slow", "linear", function() {

                                });
                            }
                        });
                    }
                });
            });

【问题讨论】:

    标签: jquery jquery-ui jquery-ui-draggable jquery-ui-droppable


    【解决方案1】:

    改你accept回调如下:

    accept: function (item) {
      return $(this).closest("tr").is(item.closest("tr")) && $(this).find("*").length == 0;
    }
    

    $(function() {
      $(".dragDiv").draggable({
        revert: 'invalid'
      });
      $(".mainTable tr td").droppable({
        accept: function(item) {
          return $(this).closest("tr").is(item.closest("tr")) && $(this).find("*").length == 0;
        },
        drop: function(event, ui) {
          var $this = $(this);
          $this.append(ui.draggable.css({
            top: 0,
            left: 0
          }));
          ui.draggable.position({
            my: "center",
            at: "center",
            of: $this,
            using: function(pos) {
              $(this).animate(pos, "slow", "linear", function() {
    
              });
            }
          });
        }
      });
    });
    .mainTable {
      margin: 20px auto;
      padding: 0px;
    }
    .mainTable tr td {
      width: 100px;
      height: 100px;
    }
    .dragDiv {
      text-align: center;
      background-color: #00ABA9;
      height: 50px;
      vertical-align: middle;
      margin: auto;
      position: relative;
      width: 50%;
    }
    <link href="http://code.jquery.com/ui/1.11.2/themes/smoothness/jquery-ui.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
    <table border="1" class="mainTable">
      <tr>
        <td id="11">
          <div class="dragDiv" id="1">N</div>
        </td>
        <td id="12">&nbsp;</td>
        <td id="13">&nbsp;</td>
      </tr>
      <tr>
        <td id="21">&nbsp;</td>
        <td id="22">
          <div class="dragDiv" id="2">N</div>
        </td>
        <td id="23">&nbsp;</td>
      </tr>
      <tr>
        <td id="31">&nbsp;</td>
        <td id="32">&nbsp;</td>
        <td id="33">
          <div class="dragDiv" id="3">N</div>
        </td>
      </tr>
    </table>

    【讨论】:

    • 你有很棒的技能...谢谢。我做到了,但我使用了 - var $this = $(this); if (($this.data("parent-id") == elm.parent().parent().attr("id")) &amp;&amp; $(this).find("*").length === 0) { return true; } else { return false; }
    • 我可以请您在这里查看一个 jQuery droppable 相关问题:stackoverflow.com/questions/51301758/… 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-10
    • 1970-01-01
    • 1970-01-01
    • 2012-08-07
    相关资源
    最近更新 更多