如何获取从哪个开始拖动的父级的 id?jQuery UI



让我们举个例子,我们有这样的代码:

$(document).ready(function(){
$(".king").draggable();
$("td").droppable({
drop: function(event, ui){
$("#target").text($(event.target).attr('id'));
}
});
});
td {
border: 1px black solid;
width: 90px;
height: 90px;
}
img {
width: 80px;
height: 80px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<table>
<tr>
<td id="drop_one"><img src="http://www.houseofchess.com/images/chess_pieces/wooden_staunton/shared/216777-375/nqw.jpg" class="king"></td>
<td id="drop_two"></td>
</tr>
<tr>
<td id="drop_tree"></td>
<td id="drop_four"></td>
</tr>
</table>
<p id="start"></p>
<p id="target"></p>

我怎么知道从哪个开始拖动td的 id。我知道首先从我放它的地方开始,但是当它移动时如何知道td拖动是从哪个开始的。是否有类似event.target的东西,以便我可以知道它从哪个对象开始?

$(".king").draggable({
var this_id = $(this).parent().attr('id');
start: function( event, ui ) {
$("#start").text(this_id);
}
});

最新更新