简体   繁体   English

jQuery拖放不起作用

[英]jQuery Drag and Drop not working

I am trying to develop a simple drag and drop 'game'. 我正在尝试开发一个简单的拖放式“游戏”。 In simple terms all you do is drag and drop various items into a area and it will say correct or wrong depending on the item dragged. 简单来说,您要做的就是将各种项目拖放到一个区域中,根据所拖动的项目,它会说对还是错。 This is what I have so far and its not working at all and I dont know why. 到目前为止,这是我所拥有的,并且根本无法正常工作,我不知道为什么。 My knowledge of JS and jQuery leaves a lot to be desired too. 我对JS和jQuery的了解也有很多不足。

<script>
$(function() {
    $( "#draggable" ).draggable();
    $( "#wrong" ).draggable();

    $( "#droppable" ).droppable({    
        drop: function( event, ui ) {
            var currentId = $(this).attr('id');
            if (currentId == "draggable") {
                $( this )
                    .addClass( "highlight" )
                    .find( "p" )
                    .html( "Correct! :)" );
            } else {
                $( this )
                    .find( "p" )
                    .html( "Wrong! :(" );
            }
        }
    }); 
});
</script>

Now that I have it working I need more instances of the draggable images but when I add more the the new ones that have been added don't work. 现在,它可以正常工作了,我需要更多可拖动图像实例,但是当我添加更多实例时,已添加的新实例将不起作用。

http://jsfiddle.net/KcruJ/9/ http://jsfiddle.net/KcruJ/9/

var currentId = $(this).attr('id');
if (currentId == "draggable")
    ...

Will never result true, as $(this) represents the droppable the draggable is dropped on. ui.draggable represents the draggable 永远不会结果为true,因为$(this) represents the droppable the draggable is dropped on. ui.draggable represents the draggable $(this) represents the droppable the draggable is dropped on. ui.draggable represents the draggable [1] $(this) represents the droppable the draggable is dropped on. ui.draggable represents the draggable [1]

Try: 尝试:

var currentId = $(ui.draggable).attr('id');
if (currentId == "draggable")
    ...

This works: http://jsfiddle.net/T6nu3/2/ 这有效: http : //jsfiddle.net/T6nu3/2/


$(this).attr('id');

Will always return droppable . 将始终返回droppable You need to access the dragged element: 您需要访问拖动的元素:

$(ui.draggable).attr('id');

Take a look at the jQuery UI Documentation for more information. 请查看jQuery UI文档以获取更多信息。

Code: 码:

$(function() {
    $("#draggable").draggable();
    $("#wrong").draggable();

    $("#droppable").droppable({
        drop: function(event, ui) {
            var currentId = $(ui.draggable).attr('id');
            if (currentId == "draggable") {
                $(this).addClass("highlight").find("p").html("Correct! :)");
            } else {
                $(this).find("p").html("Wrong! :(");
            }
        }
    });
});

haha well, Alex seems to have this one on lock. 哈哈好吧,Alex似乎已经锁定了这一功能。

There's the answer: http://jsfiddle.net/aGqHh/1/ 答案是: http//jsfiddle.net/aGqHh/1/

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

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