1

Introduction

I currently have a list (with ul and li's).

It's made sortable using jQuery:

$( "#sortable1" ).sortable({
    update : function () {
    var items = $('#sortable1').sortable('serialize');
    alert(items);                       
    }
});
$( "#sortable1" ).disableSelection();

The problem

This code moves the complete li item (including it's class - and thus it's markup).

The question

Is there any way to match the target's destination class? I've setup a demo here: http://tinker.io/65292

So basically, when moving item 1 one place down, item 1 should become green and item 2 should become red.

Additionally, when moving item 1 two places down, item 1 should be yellow, item 2 should be red and finally item 3 should be green.

This demo list only exists of four items, but realistically this could be any number.

Demo code

<!doctype html>

<html lang="en">
<head>
  <meta charset="utf-8" />
  <title>jQuery UI Sortable - Default functionality</title>
  <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
  <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
  <script src="http://code.jquery.com/ui/1.10.2/jquery-ui.js"></script>
  <link rel="stylesheet" href="/resources/demos/style.css" />
  <style>
  #sortable { list-style-type: none; margin: 0; padding: 0; width: 60%; }
  #sortable li { margin: 0 3px 3px 3px; padding: 0.4em; padding-left: 1.5em; font-size: 1.4em; height: 18px; }
  #sortable li span { position: absolute; margin-left: -1.3em; }

    .first { background-color: red; }
    .middle1 { background-color: green; }
    .middle2 { background-color: yellow; }
    .last { background-color: blue; }
  </style>
  <script>
  $(function() {
    $( "#sortable" ).sortable();
    $( "#sortable" ).disableSelection();
  });
  </script>
</head>
<body>

<ul id="sortable">
  <li class="first"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 1</li>
  <li class="middle1"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 2</li>
  <li class="middle2"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 3</li>
  <li class="last"><span class="ui-icon ui-icon-arrowthick-2-n-s"></span>Item 4</li>
</ul>


</body>
</html>

Link: http://tinker.io/65292

4

3 回答 3

3

使用 nth-child css 伪类...

 #sortable li:nth-child(1) { background-color: red; }
#sortable li:nth-child(2) { background-color: green; }
#sortable li:nth-child(3) { background-color: yellow; }
#sortable li:nth-child(4)  { background-color: blue; }
于 2013-04-10T15:36:22.270 回答
2

将 css 更改为基于索引而不是类的目标列表元素,它将自动更新:

#sortable li:first-child { background-color: red; }
#sortable li:nth-child(2) { background-color: green; }
#sortable li:nth-child(3) { background-color: yellow; }
#sortable li:last-child { background-color: blue; }

修补匠

于 2013-04-10T15:36:29.773 回答
1

jQuery UI 的 Sortable 是基于移动整个 DOM 元素的原理。

正如您正确注意到的那样,整个 DOM 对象都被移动了,包括它的所有内容和所有属性,因此也包括它的类。

我想到了几种方法:

  1. 更改您的 CSS,使其不需要<li>s 上的类。可以使用纯 CSS 来制作第一个红色、第二个绿色等。您可以以各种方式使用nth-childor+或选择器。~然后移动<li>s 就可以了,它们落入到位时的外观是正确的。
  2. 在每个元素移动后直接使用 javascript 删除和重新应用类。
于 2013-04-10T15:39:02.257 回答