【问题标题】:get the start position of an item using the jquery ui sortable plugin使用 jquery ui 可排序插件获取项目的开始位置
【发布时间】:2010-04-12 08:02:33
【问题描述】:
我正在使用 jQuery UI 可排序插件,我正在尝试获得 2 个警报
我想要元素的起始位置和元素的完成位置。
$(function() {
$("#filterlist ul").sortable({ opacity: 0.6, cursor: 'move', update: function(event, ui) {
alert(ui.item.prevAll().length + 1);
}
});
});
我可以通过以下方式获取项目被拖动后的位置:-
ui.item.prevAll().length + 1
我用什么来获得它开始的位置?
【问题讨论】:
标签:
jquery
jquery-ui-sortable
【解决方案1】:
$(function() {
$("#sortable").sortable({
start: function(event, ui) { console.log('before @ '+ ui.item.index()) },
update: function(event, ui) { console.log('now @ '+ ui.item.index()) }
});
});
试试这个demo 并在控制台观看...
【解决方案2】:
使用start 事件并“缓存”起始位置
var start;
$(function() {
$("#filterlist ul").sortable({
opacity: 0.6,
cursor: 'move',
start: function(event, ui) {
start = ui.item.prevAll().length + 1;
},
update: function(event, ui) {
alert(start + " -> " + (ui.item.prevAll().length + 1));
}
});
})