将不同列表中的两个可排序对象按其类移动到相同位置

Use*_*643 8 javascript arrays jquery jquery-ui-sortable

我有两个可排序对象列表

1         1 (1A 1B)
2 (2A 2B) 2 
3         3 (3A 3B)
4 (4A 4B) 4
5         5 (5A 5B)
Run Code Online (Sandbox Code Playgroud)

列表的代码如下所示:

1         1 (1A 1B)
2 (2A 2B) 2 
3         3 (3A 3B)
4 (4A 4B) 4
5         5 (5A 5B)
Run Code Online (Sandbox Code Playgroud)
$( function() {
    $( ".contain" ).sortable();
 });
Run Code Online (Sandbox Code Playgroud)
.contain{
   list-style: none;
}

#right{
   float: left;
}

#left{
   float: left;
}
Run Code Online (Sandbox Code Playgroud)

我想将相同的数字排序在一起。例如,如果我将列表5中​​的“左”移到顶部,那么列表5中的“右”也应移到顶部,反之亦然。从列表“ right”中的3移到顶部,然后列表“ left”中的3将执行相同的操作。

Udh*_*tus 4

在你的代码中使用这个js并尝试这个答案

$(".contain").sortable({
    start:function(event, ui){
        pre = ui.item.index();
    },
    stop: function(event, ui) {
        lst = $(this).attr('id');
        post = ui.item.index();
        other = (lst == 'left') ? 'right' : 'left';
        if (post > pre) {
            $('#'+other+ ' li.li_parent:eq(' +pre+ ')').insertAfter('#'+other+ ' li.li_parent:eq(' +post+ ')');
        }else{
            $('#'+other+ ' li.li_parent:eq(' +pre+ ')').insertBefore('#'+other+ ' li.li_parent:eq(' +post+ ')');
        }
    }
});
Run Code Online (Sandbox Code Playgroud)

您的 ul 应该与 li 一起使用,例如:-并为 moving<li>1<ul><li>a</li></ul></li>指定类名。li_parentli

$(".contain").sortable({
    start:function(event, ui){
        pre = ui.item.index();
    },
    stop: function(event, ui) {
        lst = $(this).attr('id');
        post = ui.item.index();
        other = (lst == 'left') ? 'right' : 'left';
        if (post > pre) {
            $('#'+other+ ' li.li_parent:eq(' +pre+ ')').insertAfter('#'+other+ ' li.li_parent:eq(' +post+ ')');
        }else{
            $('#'+other+ ' li.li_parent:eq(' +pre+ ')').insertBefore('#'+other+ ' li.li_parent:eq(' +post+ ')');
        }
    }
});
Run Code Online (Sandbox Code Playgroud)
$(function() {
    var lst, pre;

    $(".contain").sortable({
        start: function(event, ui) {
            pre = ui.item.index();
        },
        stop: function(event, ui) {
            lst = $(this).attr('id');
            post = ui.item.index();
            other = (lst == 'left') ? 'right' : 'left';
            if (post > pre) {
                $('#' + other + ' li.li_parent:eq(' + pre + ')').insertAfter('#' + other + ' li.li_parent:eq(' + post + ')');
            } else {
                $('#' + other + ' li.li_parent:eq(' + pre + ')').insertBefore('#' + other + ' li.li_parent:eq(' + post + ')');
            }
        }
    });

});
Run Code Online (Sandbox Code Playgroud)
.contain{
   list-style: none;
}

#right{
   float: left;
}

#left{
   float: left;
}
Run Code Online (Sandbox Code Playgroud)