拖放问题jquery

时间:2014-07-29 15:29:17

标签: javascript jquery jquery-ui

我在下面的代码使用脚本我正在制作可拖动和可放置的东西。目前我有两个可拖动内容的部分,两个地方可以删除,如果正确,那么系统将改变并说出正确的,我想知道是否有,我将能够只创建HTML divs和代码运行通过并将draggable与dropable相匹配。我的人有非技术性的bac​​kdround,有基本的HTML知识,所以你现在如何添加div并删除它们但他们能够处理脚本,我想知道,如果我的ID为1-10的可拖动的内容和相同的可丢弃的1-10所以id 1 draggable只能添加到id一个droppable。

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

2 个答案:

答案 0 :(得分: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);
            }
        });
    });
});

答案 1 :(得分:0)

如果我正确理解你的问题,你希望每个可拖动的只能放置它的droppable,而不是其他可放弃的div。

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

你可以添加这个额外的代码行,这样如果你的draggable被丢弃到droppable以外的任何地方,它将返回到它的droppable。 $( "#draggable, #draggable2" ).draggable({ revert: "invalid" });

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

以下是显示示例的a jsfiddle