【问题标题】:drag and drop issue jquery拖放问题jquery
【发布时间】:2014-07-30 00:08:42
【问题描述】:

我有下面的代码使用我正在制作可拖放和可拖放的东西的脚本。目前我有两个用于可拖动内容的部分,如果正确则可以将它们放置在两个位置,然后系统会更改并说正确,我想知道是否有可能只创建 HTML div 并运行代码通过并将可拖动对象与可拖放对象匹配。我有些人有非技术背景,有基本的 HTML 知识,所以你现在如何添加 div 和删除它们,但他们能够处理脚本,我想知道,如果我的 ID 为 1-10可拖动内容和可拖放的 1-10 相同,因此 id 1 可拖动只能添加到 id 1 可拖放。

<meta charset="utf-8">
  <link rel="stylesheet" href="https://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
  <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
  <script src="https://code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
  <link rel="stylesheet" href="hhtps:/resources/demos/style.css">
  <style>
  #droppable,#droppable2 { width: 150px; height: 150px; padding: 0.5em; float: left; margin: 10px; }
  #draggable, #draggable2 { width: 100px; height: 100px; padding: 0.5em; float: left; margin: 10px 10px 10px 0; }
  </style>
  <script>
  $(function() {
    $( "#draggable, #draggable2" ).draggable();


    $( "#droppable" ).droppable({
      accept: "#draggable",
      drop: function( event, ui ) {
        $( this )
          .removeClass("ui-widget-header")
          .addClass( "ui-state-highlight" )
          .find( "p" )
            .html( "Dropped!" );
      },
      out: function( event, ui ) {
        $( this ).removeClass( "ui-state-highlight" ).addClass( "ui-widget-header" )
          .find( "p" )
            .html( "accept" );
      }
    });

  $( "#droppable2" ).droppable({
      accept: "#draggable2",
      drop: function( event, ui ) {
        $( this )
          .removeClass("ui-widget-header")
          .addClass( "ui-state-highlight" )
          .find( "p" )
            .html( "Dropped!" );
      },
      out: function( event, ui ) {
        $( this ).removeClass( "ui-state-highlight" ).addClass( "ui-widget-header" )
          .find( "p" )
            .html( "accept" );
      }
    });

  });
  </script>

<div id="draggable2" class="ui-widget-content">
  <p>I'm draggable but can't be dropped</p>
</div>

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

<div id="droppable" class="ui-widget-header">
  <p>accept: '#draggable'</p>
</div>

<div id="droppable2" class="ui-widget-header">
  <p>accept: '#draggable2'</p>
</div>

【问题讨论】:

  • “创建 div 并匹配可拖动到可放置”是什么意思?我没有得到你想要达到的目标。

标签: javascript jquery jquery-ui


【解决方案1】:

为此,您需要避免使用 HTML id 并开始使用类。以下是如何做到这一点,working example:

您的 HTML:

<div id="draggable_2" class="ui-widget-content draggable-item">
    <p>draggable_2</p>
</div>
<div id="draggable" class="ui-widget-content draggable-item">
    <p>draggable</p>
</div>
<div class="ui-widget-header droppable-item" data-accept="#draggable">
    <p></p>
</div>
<div class="ui-widget-header droppable-item" data-accept="#draggable_2">
    <p></p>
</div>
<div class="ui-widget-header droppable-item" data-accept="#draggable_3">
    <p></p>
</div>

你的javascript:

$(function () {
    $(".draggable-item").draggable();

    $('.droppable-item').each(function (i, ele) {
        // Gets the accepted from the HTML property "data-accept"
        var accept = $(this).data('accept');

        // This is just to show what the item accepts. you can remove it.
        $(this).find('p').text('accepts: ' + accept);

        // Init the jQuery UI droppable()
        $(this).droppable({
            accept: accept,
            drop: function (event, ui) {
                $(this)
                    .removeClass("ui-widget-header")
                    .addClass("ui-state-highlight")
                    .find("p")
                    .html("Dropped!");
            },
            out: function (event, ui) {
                $(this).removeClass("ui-state-highlight").addClass("ui-widget-header")
                    .find("p")
                .html("accept: " + accept);
            }
        });
    });
});

【讨论】:

    【解决方案2】:

    如果我正确理解了您的问题,您希望每个可拖动对象只能放置其可放置的 div,而不能放置其他可放置的 div。

    您已经通过在代码中为每个droppable 添加accept: "#draggable" 来实现这一点。

    您可以添加这行额外的代码,这样如果您的可拖动对象被拖放到其可拖放对象以外的任何位置,它将返回到其可拖放对象。 $( "#draggable, #draggable2" ).draggable({ revert: "invalid" });

    如果您将相同的类(如 class=draggables 添加到每个可拖动的 html 元素中,则可以缩短此代码,然后您可以使用 $( ".draggables" ).draggable({ revert: "invalid" }); 全部标记。

    这里是a jsfiddle 来展示示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-16
      • 2019-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-21
      • 2011-02-25
      相关资源
      最近更新 更多