문제

I need to get elements id and class when something is dropped on it. Id works fine, but class is not passed...

HTML

<div id="drag" class="two">drag</div>
<div id="drop" class="one">drop</div>

JS

$(document).ready(function(){
    $("#drag").draggable();
    $("#drop").droppable({
        drop: function(){
            alert(this.class+" "+this.id)
        }
    });

jsfiddle

도움이 되었습니까?

해결책

http://jsfiddle.net/FLLaj/

As mentioned. when Draggable is dropped, it retrieved the class. but there's 2 classes. A much proper way of doing is to use data-* attribute introduced in HTML5

 $(document).ready(function(){
        $("#drag").draggable({revert: true})
        $("#drop").droppable({
            drop: function(){
                alert($(this).attr('class')+" "+$(this).attr('id'))
            }})
       })

다른 팁

There's no class property - use className to retrieve the class. But as has been noted in the comments, jQuery UI adds its own classes as well.

Instead of class you can use any rel attributes.Because class names are not unique.For Example

<div id="drag" class="two" rel="u1">drag</div>
<div id="drop" class="one"rel="u2">drop</div>

JS COde:
$(document).ready(function(){
 $("#drag").draggable({revert: true})
 $("#drop").droppable({
    drop: function(){
        alert($(this).attr('rel'));
    }})
})

Link Here

$(document).ready(function(){
   $("#drag").draggable({revert: true})
   $("#drop").droppable({
       drop: function(event,ui){
           console.log(ui.draggable.attr('class'));
           console.log($(this).attr('class'));
       }
   });
});

modified your code.check if it works

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top