Interfaz de usuario de JQuery: ¿es posible saber dónde se ha dejado caer un objeto?

what I want to do is to know where (not in terms of position (x, y), but a reference to the DOM element) an object was dropped.

I have a grid made up with divs where you can drop various items and I need to know which div on the grid was the item dropped on (getting its id would be fine). The callback function

function(event, ui) { //code here }

has just that ui object who doesn't apparently contain any information about this, but only about the draggable item or its helper.

preguntado el 09 de enero de 11 a las 10:01

1 Respuestas

What you need to use is the jQuery Droppable class, which you can read about in the documentos de jQuery.

Basically, for every element that you wish to be able to drop a draggable element on, you create a Droppable object, which will trigger once another object is dragged onto it. This is the example from the jQuery docs:

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

La $(this) in that example is the element you are looking for.

Respondido el 09 de enero de 11 a las 14:01

Thank you, I don't really know how I've missed this :) - El monje de la codificación

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.