我在我的一個項目中使用了類似的函數,並且準備好了一段代碼來恢復排序。該函數採用2個參數:
- 列表容器
- 保持逗號該Cookie分離的ID。
我用它來重新安排UL根據名單,但應該工作在你的情況太細...
// Function that restores the list order from a cookie
function restoreOrder(_list, _cookie) {
var list = $('#' + _list);
if(list == null) return;
// fetch the cookie value (saved order)
var cookie = $.cookie(_cookie);
if(!cookie) return;
// make array from saved order
var IDs = cookie.split(",");
// fetch current order
var items = list.sortable("toArray");
// make array from current order
var rebuild = new Array();
for(var v = 0, len = items.length; v < len; v++)
rebuild[items[v]] = items[v];
for(var i = 0, n = IDs.length; i < n; i++) {
// item id from saved order
var itemID = IDs[i];
if(itemID in rebuild) {
// select item id from current order
var item = rebuild[itemID];
// select the item according to current order
var child = $('#' + _list).children('#' + item);
// select the item according to the saved order
var savedOrd = $('#' + _list).children('#' + itemID);
// remove all the items
child.remove();
// add the items in turn according to saved order
// we need to filter here since the "ui-sortable"
// class is applied to all ul elements and we
// only want the very first! You can modify this
// to support multiple lists - not tested!
$('#' + _list).filter(':first').append(savedOrd);
} // if
} // for
} // restoreOrder
我已經忘記了原來的(一些論壇或博客,就知道了而谷歌搜索)...但由於原作者得分。我已經修改了原來的一些例程,以便通過接受這些參數使它更加可重用。
乾杯, 微觀^世人
是的,但我怎麼retreive的DOM元素的順序? – Cudos 2009-05-25 09:30:28
就像那樣。這很簡單。 – Oli 2009-05-25 10:00:35