可拖放功能

发布于 2024-12-18 18:25:02 字数 737 浏览 4 评论 0原文

假设我的左侧有一些可放置的 div,标题为区域 1 - 4。右侧有一些

<div id="zone1"></div>
<div id="zone2"></div>
<div id="zone3"></div>
<div id="zone4"></div>

可拖动的项目,

<div id="drag1">Apple</div>
<div id="drag2">Banana</div>
<div id="drag3">Kiwi</div>
<div id="drag4">Orange</div>

我知道我可以将它们设置为可拖动和可放置,如下所示:

for (var i=1;i<=4;i++)
{
   var x = "zone" + i;
   $(x).droppable();
}
  for (var i=1;i<=4;i++)
{
   var x = "drag" + i;
   $(x).draggable();
}

现在我将如何触发当水果掉落在某个区域时启动的函数,该函数将告诉我掉落的位置(区域 1、区域 2 等)以及掉落的水果。理想情况下,我不需要水果名称,我想要一个与水果相关联的ID,可能隐藏在隐藏的输入标签或其他东西中?

Let's say I have some droppable div's on my left hand side titled zone 1 - 4.

<div id="zone1"></div>
<div id="zone2"></div>
<div id="zone3"></div>
<div id="zone4"></div>

And some draggable items on my right hand side

<div id="drag1">Apple</div>
<div id="drag2">Banana</div>
<div id="drag3">Kiwi</div>
<div id="drag4">Orange</div>

I know that I can set these up as draggable and droppable as follows:

for (var i=1;i<=4;i++)
{
   var x = "zone" + i;
   $(x).droppable();
}
  for (var i=1;i<=4;i++)
{
   var x = "drag" + i;
   $(x).draggable();
}

Now how would I go about triggering a function to start when a fruit is dropped in a zone that will give me both where it was dropped (zone1, zone2 etc.) as well as which fruit was dropped there. Ideally, I wouldn't want the fruit name, I would want an id associated with the fruit perhaps hidden in a hidden input tag or something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

娇女薄笑 2024-12-25 18:25:02

我建议你这样做,而不是你现有的。

<div id="zone1" class="droppable"></div>
<div id="zone2" class="droppable"></div>
<div id="zone3" class="droppable"></div>
<div id="zone4" class="droppable"></div>

<div id="drag1" class="draggable">Apple</div>
<div id="drag2" class="draggable">Banana</div>
<div id="drag3" class="draggable">Kiwi</div>
<div id="drag4" class="draggable">Orange</div>

然后是你的脚本:

var fruit_id = '';
var dropbox_id = '';
$( '.draggable' ).draggable();
$( '.droppable' ).droppable({
               drop: function( event, ui ) {
            fruit_id = $(ui.draggable).attr('id');
                            dropbox_id = $( this ).attr('id');
                            /* DO SOMETHING HERE */
        }
});
/* Or do something here! */

希望有帮助!

I suggest that you do this instead of what you have.

<div id="zone1" class="droppable"></div>
<div id="zone2" class="droppable"></div>
<div id="zone3" class="droppable"></div>
<div id="zone4" class="droppable"></div>

<div id="drag1" class="draggable">Apple</div>
<div id="drag2" class="draggable">Banana</div>
<div id="drag3" class="draggable">Kiwi</div>
<div id="drag4" class="draggable">Orange</div>

Then for your script:

var fruit_id = '';
var dropbox_id = '';
$( '.draggable' ).draggable();
$( '.droppable' ).droppable({
               drop: function( event, ui ) {
            fruit_id = $(ui.draggable).attr('id');
                            dropbox_id = $( this ).attr('id');
                            /* DO SOMETHING HERE */
        }
});
/* Or do something here! */

Hope that helps!

已下线请稍等 2024-12-25 18:25:02

本教程中的示例

$(init)

function init() {
  $('#makeMeDraggable').draggable();
  $('#makeMeDroppable').droppable( {
    drop: handleDropEvent
  } );
}

function handleDropEvent( event, ui ) {
  var draggable = ui.draggable;
  alert( 'The square with ID "' + draggable.attr('id') + '" was dropped onto me!' );
}

An example from this tutorial

$(init)

function init() {
  $('#makeMeDraggable').draggable();
  $('#makeMeDroppable').droppable( {
    drop: handleDropEvent
  } );
}

function handleDropEvent( event, ui ) {
  var draggable = ui.draggable;
  alert( 'The square with ID "' + draggable.attr('id') + '" was dropped onto me!' );
}
素食主义者 2024-12-25 18:25:02

您可以使用 droppable 交互的 drop 事件。在此事件处理程序中,$(this) 指向可放置元素,其中 ui.draggable 指向可拖动元素。

for (var i=1;i<=4;i++)
{
   var x = "zone" + i;
   $(x).droppable({
      drop: function(){
          //This will alert the id of container in which fruit is dropped
          alert($(this).attr('id'));

          //This will alert the id of fruit container which is dragged
          alert($(ui.draggable).attr('id'));
      }
   });
}
  for (var i=1;i<=4;i++)
{
   var x = "drag" + i;
   $(x).draggable();
}

You can use drop event of the droppable interaction. In this event handler $(this) points to the droppable element where as ui.draggable points to the draggable element.

for (var i=1;i<=4;i++)
{
   var x = "zone" + i;
   $(x).droppable({
      drop: function(){
          //This will alert the id of container in which fruit is dropped
          alert($(this).attr('id'));

          //This will alert the id of fruit container which is dragged
          alert($(ui.draggable).attr('id'));
      }
   });
}
  for (var i=1;i<=4;i++)
{
   var x = "drag" + i;
   $(x).draggable();
}
浅浅淡淡 2024-12-25 18:25:02
<style>
.zones>div{ border:solid 1px gray; background-color:yellow; padding:20px;margin:5px;}
</style>

<div class="zones">
    <div id="zone1"></div>
    <div id="zone2"></div>
    <div id="zone3"></div>
    <div id="zone4"></div>
</div>


<div id="drag1" fruitid="1">Apple</div>
<div id="drag2" fruitid="2">Banana</div>
<div id="drag3" fruitid="3">Kiwi</div>
<div id="drag4" fruitid="4">Orange</div>

<script>

$("div[id*='zone']").droppable({
            accept: "div[id*='drag']",
            drop: function( event, ui ) {
                alert( ui.draggable.text() + " with fruitid = "+ ui.draggable.attr("fruitid")+" added to " + $(this).attr("id"));
                  $( "<div/>" ).text( ui.draggable.text()).appendTo( this );
            }
        })
$('div[id*="drag"]').draggable({helper: "clone"});

</script>
<style>
.zones>div{ border:solid 1px gray; background-color:yellow; padding:20px;margin:5px;}
</style>

<div class="zones">
    <div id="zone1"></div>
    <div id="zone2"></div>
    <div id="zone3"></div>
    <div id="zone4"></div>
</div>


<div id="drag1" fruitid="1">Apple</div>
<div id="drag2" fruitid="2">Banana</div>
<div id="drag3" fruitid="3">Kiwi</div>
<div id="drag4" fruitid="4">Orange</div>

<script>

$("div[id*='zone']").droppable({
            accept: "div[id*='drag']",
            drop: function( event, ui ) {
                alert( ui.draggable.text() + " with fruitid = "+ ui.draggable.attr("fruitid")+" added to " + $(this).attr("id"));
                  $( "<div/>" ).text( ui.draggable.text()).appendTo( this );
            }
        })
$('div[id*="drag"]').draggable({helper: "clone"});

</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文