jQuery UI Draggable/Sortable - Get reference to new item

前端 未结 3 2001
半阙折子戏
半阙折子戏 2020-12-14 19:31

I am using the jQuery UI Draggable/Sortable demo (http://jqueryui.com/demos/draggable/#sortable) for the basis of my project. I need to get a reference to the

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-14 20:22

    If you don't fear accessing the Sortable's internal state, you can do the following:

    $('#sortable').sortable({
      receive: function() {
        // Sortable.currentItem refers to the item just added.
        // jQuery UI < 1.10 uses "sortable" as its data key,
        // jQuery UI >= 1.10 defines an "instance" method to get the current instance.
        var $item =
          (
            $('#sortable').data('sortable') || // jQuery UI < 1.10
            $('#sortable').sortable('instance') // jQuery UI >= 1.10
          ).currentItem;
      }
    );
    

    Fiddle: http://jsfiddle.net/t33bt/12/

    Keep in mind though, that this might not work in a future jQuery UI version anymore because it doesn't use the official API. But nonetheless it works even in jQuery UI 1.11 (current version as of time of this writing).

提交回复
热议问题