可拖放功能
假设我的左侧有一些可放置的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议你这样做,而不是你现有的。
然后是你的脚本:
希望有帮助!
I suggest that you do this instead of what you have.
Then for your script:
Hope that helps!
本教程中的示例
An example from this tutorial
您可以使用
droppable
交互的drop
事件。在此事件处理程序中,$(this)
指向可放置元素,其中 ui.draggable 指向可拖动元素。You can use
drop
event of thedroppable
interaction. In this event handler$(this)
points to the droppable element where as ui.draggable points to the draggable element.