如何获取draggable元素的类名?

时间:2014-05-29 12:27:19

标签: jquery-ui

在droppable函数中,如何获取当前被拖动的可拖动元素的类名。

$( ".drop" ).droppable( { accept: handleFunction } );
function handleFunction(draggable){
//How to get currently dragging element's class name
}

1 个答案:

答案 0 :(得分:2)

我认为您没有正确使用accept选项的用法。它没有得到当前拖动的可拖动,它测试你刚刚放在droppable元素中的可拖动。来自the droppable documentation

Function: A function that will be called for each draggable
on the page (passed as the first argument to the function).
The function must return true if the draggable should be accepted.

无论如何,作为参数获得的是jQuery元素,而不是JS对象。您可以通过多种方式测试其类:

$( ".drop" ).droppable( { accept: handleFunction } );
function handleFunction(draggable){
     // This will return ALL classes attached to this draggable element as a string
     // This is probably NOT what you need; it's VERY fragile
     var className = $(draggable).attr('class');

     // This returns a boolean for testing if myClass is present in the element's classes
     // This is a very common function and probably what you want
     var test = $(draggable).hasClass('myClass');

     // Note return value should be a boolean.
     return test;
}
相关问题