jQuery可排序列表在创建时不重新排序

时间:2014-07-13 00:02:00

标签: javascript jquery jquery-ui

当我根据列表中的顺序创建列表中的元素时,我试图重新排序列表中元素的元素。

var CACHES = ["WayBack Machine", "Google Cache", "Coral CDN"];
var HTML = ["Google Cache", "WayBack Machine", "Coral CDN"];

$("#sortable").sortable({
    stop: function(event, ui) {
        console.log("New position: " + ui.item.index());
    },
    change: function(event, ui) {
        // When the item postion is changed while dragging a item, I want it's position
        console.log("New position: " + ui.item.index());
    },
    create: function(event, ui) {
        console.log("Sortable Created");

        var ul = $('#sortable');
        var li = ul.children('li').get();

        li.sort(function(a,b) {
            $(a).attr('id')
            var value = CACHES.indexOf($(a).attr('id')) < CACHES.indexOf($(b).attr('id')) ? 1 : -1
            console.log(value)
            return value
        });

        ul.append(li);
    }
});

var HTML是它们在页面HTML中的顺序。 var CACHES是我想要它们的顺序。它似乎对元素重新排序,但不正确。

1 个答案:

答案 0 :(得分:1)

http://www.w3schools.com/jsref/jsref_sort.asp

使用 - 而不是&lt;和三元

var value = CACHES.indexOf($(a).attr('id')) - CACHES.indexOf($(b).attr('id'));
相关问题