javascript-对列表项进行向上或向下排序

我在下面尝试做的一个非常简单的例子.左侧的链接/按钮用于向上移动项目,右侧的链接/按钮用于向下移动项目.但它不起作用,并且我收到一个错误:

Object doesnt support this property or method

在这一行:

items[counter-1] = curr;// move
previous item, to current

示例图片:

javascript-对列表项进行向上或向下排序

这是我的代码:

function MoveItem(id, direction) {
  var ul = document.getElementById('GroupBy');
  var items = ul.getElementsByTagName('li');

  var counter = 0;
  var previousItem = null;
  var moveNextItemUp = false;

  for (var item in items) {

    //if current item, is the one to be moved
    if (item == id) {
      if (direction == 1) { // item move down
        moveNextItemUp = true;
      } else if ((direction == -1) || (moveNextItemUp == true)) { // item move up
        var curr = items[counter];
        var prev = items[counter - 1];

        items[counter - 1] = curr; // move previous item, to current
        items[counter] = prev; //move current item, to previous

        break;
      }
    }

    previousItem = item;
    counter = counter + 1;
  }

}
<ul id="GroupBy">
  <li id="One">
    one<a href="#" onclick="MoveItem('One', 1)"> V </a>
  </li>
  <li id="Two">
    <a href="#" onclick="MoveItem('Two', -1)"> ^ </a>two<a href="#" onclick="MoveItem('Two', 1)"> V </a>
  </li>
  <li id="Three">
    <a href="#" onclick="MoveItem('Three', -1)"> ^ </a>three<a href="#" onclick="MoveItem('Three', 1)"> V </a>
  </li>
  <li id="Four">
    <a href="#" onclick="MoveItem('Four', -1)"> ^ </a>four
  </li>
</ul>

解决方法:

您使用的是for-in语句,这意味着您无法保证可能期望的数字顺序.

使用for语句代替:

for (var i = 0, len = items.length; i < len; i++) {

另外,请记住,项是“活动列表”,因此您在DOM中所做的更改将反映在列表中,并且列表本身不是数组,因此它是不可变的.

如果要将元素移回一个索引,请使用insertBefore.

像这样:

items[i].parentNode.insertBefore( items[i],items[i-1] );

例如:http://jsfiddle.net/d25a3/

function MoveItem(id, direction) {
    var ul = document.getElementById('GroupBy');
    var items = ul.getElementsByTagName('li');

    var counter = 0;
    var previousItem = null;
    var moveNextItemUp = false;

    for (var i = 0, len = items.length; i < len; i++) {
        var item = items[i];

        if (item.id == id) {
            if (direction == 1) { 
                moveNextItemUp = true;
            } else if ((direction == -1) || (moveNextItemUp == true)) {

                item.parentNode.insertBefore( item,items[i-1] );
                break;
            }
        }
        previousItem = item;
        counter = counter + 1;
    }
}

另外,不确定代码的全部意图是什么,但是您似乎可以简化如下代码:

示例:http://jsfiddle.net/d25a3/1/

   <!-- pass the parent node of the item clicked as the first argument -->

<li id="Two">
    <a href="#" onclick="MoveItem(this.parentNode, -1)"> ^ </a>two<a href="#" onclick="MoveItem('Two', 1)"> V </a>
</li>

并完全摆脱循环:

function MoveItem(item, direction) {

    var counter = 0;
    var previousItem = null;
    var moveNextItemUp = false;

    if (direction == 1) {
        moveNextItemUp = true;
    } else if ((direction == -1) || (moveNextItemUp == true)) {

           // get the previous <li> element
        var prev = item.previousSibling
        while( prev && prev.nodeType != 1 && (prev = prev.previousSibling));

        item.parentNode.insertBefore(item, prev);
    }
    previousItem = item;
    counter = counter + 1;
}
上一篇:在一组数据上使用Java不同的排序方法


下一篇:如何从PHP中的值数组中排序特定的字符串?