简体   繁体   English

如何在jQuery Droppable事件中做些什么

[英]How to do something in jquery droppable event

Hi all sorry if its a silly question.But I am new to javascript. 大家好,抱歉,如果这是一个愚蠢的问题。但是,我是javascript新手。

I have a div on which is draggable.Now I want to do some thing when that div is dropped So far I have write a simple alert but its not working. 我有一个可拖动的div。现在我想在删除该div时做一些事情到目前为止,我已经编写了一个简单的警报,但它不起作用。 Can any one guide me how do I write some code in drop event. 谁能指导我如何在drop事件中编写一些代码。

Here is my code 这是我的代码

<div id = "par" class = "ui-draggable" style="position: relative; height: 200px; width: 200px;outline:2px solid green">
</div>

<script>
     $('#par').draggable();
     $('#par').droppable({
            drop: function( event, ui ) {
                  alert("blabla");                                       
            }
     });
</script>

Here is link to fiddle Draggable droppable 这里是链接捣鼓可拖动可弃

Like MrObrian says, you need to be using draggable.stop for what you are tying to achieve. 就像MrObrian所说,您需要使用draggable.stop来实现目标。

This fiddle illustrates the difference. 这个小提琴说明了差异。 http://jsfiddle.net/MddyD/4/ http://jsfiddle.net/MddyD/4/

$(function() {
 $('#par').draggable({
               stop: function() { 
                       alert("hi");
                     }
            });

 $("#dropIn").droppable({
               drop: function() { 
                       alert("drop");
                     }
               });        

Use Droppable.drop to catch what was dropped on droppable(You need to use both draggable and droppable) 使用Droppable.drop捕获放置在droppable上的内容(您需要同时使用Draggable和Droppable)

http://docs.jquery.com/UI/Droppable#event-drop http://docs.jquery.com/UI/Droppable#event-drop

<script>
     $('#thingToDrag").draggable();
     $('#par').droppable({
            accept: "#thingToDrag",
            drop: function( event, ui ) {
                  alert("draggable dropped");                                       
            }
     });
</script>

You need to use the dragstop event which will be launched when the draggable element will stop being dragged : 您需要使用dragstop事件,该事件将在draggable元素停止拖动时启动:

$('#par')
.draggable()
.on('dragstop', function(event, ui) {
    alert('ok')
});

http://jsfiddle.net/MddyD/3/ http://jsfiddle.net/MddyD/3/

Here, you need to work with the object you setup as draggable. 在这里,您需要使用设置为可拖动的对象。 You can bind a function to the stop event. 您可以将函数绑定到stop事件。

<script>
    $(function() {
        $('#par').draggable({
            stop: function(event, ui) {
                alert("Dropped");
            }
        });
    });
</script>

the $(function(){ }); $(function(){}); just tells the script to run when the document is done loading. 只是告诉脚本在文档加载完成后运行。 You could also just bind any other function that you create to the stop event. 您也可以将您创建的任何其他函数绑定到stop事件。

http://jqueryui.com/demos/draggable/#event-stop http://jqueryui.com/demos/draggable/#event-stop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM