如何多次运行可放置函数

时间:2018-07-11 15:17:05

标签: jquery jquery-ui drag-and-drop jquery-ui-draggable jquery-ui-droppable

到目前为止,我有两张可拖动的图像(一男一女)。当我拖动男人时,我希望可拖放图像更改为男人,而对于女人则相同。到目前为止,它仍然有效,但可以说我已经将可放置图像更改为男人。然后,当我将女人拖到上方时,它不会切换为女人的图像。它仅停留在更改后的第一张图片上,我希望用户能够多次拖动和更改图片。

    <div class="types">
    <h1>TWO TYPES OF HARRASSMENT</h1>
    <p>Click and drag the employees to reveal more information.</p>
    <img class="draggable-woman" src="images/drag-woman.png" />
    <img class="draggable-man" src="images/drag-man.png" />
    </br>
    <div class="types-child">
        <img class="quid" src="images/quid-empty.png" />
        <div class="changeable-text"></div>
        <img class="hostile" src="images/hostile-empty.png" />
    </div>
</div>

<script>
$(".draggable-woman").draggable({
    helper: "clone"
  });
  $(".draggable-man").draggable({
    helper: "clone"
  });
$(".quid").droppable({
    drop: function( event, ui ) {
        if ($(ui.draggable).hasClass("draggable-woman")){
        $(this).replaceWith("<img class='quid' src='images/quid-withwoman.png'>");

        } else {
        $(this).replaceWith("<img class='quid' src='images/quid-withman.png'>")
        }
    }
});
</script>

1 个答案:

答案 0 :(得分:0)

只需要重新处理事件。这可能对您有用:

$(".draggable-woman").draggable({
    helper: "clone"
});

$(".draggable-man").draggable({
helper: "clone"
});

function handleQuidClass(){
    $(".quid").droppable({
    drop: function( event, ui ) {
            if ($(ui.draggable).hasClass("draggable-woman")){
                $(this).replaceWith("<img class='quid' src='images/quid-withwoman.png'>");
            } else {
                $(this).replaceWith("<img class='quid' src='images/quid-withman.png'>")
            }
            handleQuidClass();
        }
    });
}

handleQuidClass();
相关问题