jQuery UI Sortable,如何在更新事件中确定当前位置和新位置? [英] jQuery UI Sortable, how to determine current location and new location in update event?

查看:106
本文介绍了jQuery UI Sortable,如何在更新事件中确定当前位置和新位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

<ul id="sortableList">
     <li>item 1</li>
     <li>item 2</li>
     <li>item 3</li>
</ul>

我已连接到update: function(event, ui) { },但不确定如何获取元素的原始位置和新位置.如果我将项目3移到项目1上方,则我希望原始位置为 2 (从0开始的索引),项目3的新位置为 0 . /p>

I have wired into the update: function(event, ui) { } but am not sure how to get the original and new position of the element. If i move item 3 to be above item 1, I want the original position to be 2 (0 based index) and the new position of item 3 to be 0.

推荐答案

$('#sortable').sortable({
    start: function(e, ui) {
        // creates a temporary attribute on the element with the old index
        $(this).attr('data-previndex', ui.item.index());
    },
    update: function(e, ui) {
        // gets the new and old index then removes the temporary attribute
        var newIndex = ui.item.index();
        var oldIndex = $(this).attr('data-previndex');
        $(this).removeAttr('data-previndex');
    }
});

这篇关于jQuery UI Sortable,如何在更新事件中确定当前位置和新位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆