代码之家  ›  专栏  ›  技术社区  ›  Kosmo零

如何更改JS中的子元素顺序?

  •  8
  • Kosmo零  · 技术社区  · 10 年前

    我有这个html:

    <table>
        <tr>
            <td>
                Set right order
            </td>
            <td>
                <span style = "display: block;">asd | <a href = '#' onclick = "moveChoiceTo(this, -1);">&uarr;</a><a href = '#' onclick = "moveChoiceTo(this, 1);">&darr;</a></span>
                <span style = "display: block;">dsa | <a href = '#' onclick = "moveChoiceTo(this, -1);">&uarr;</a><a href = '#' onclick = "moveChoiceTo(this, 1);">&darr;</a></span>
                <span style = "display: block;">qwe | <a href = '#' onclick = "moveChoiceTo(this, -1);">&uarr;</a><a href = '#' onclick = "moveChoiceTo(this, 1);">&darr;</a></span>
                <span style = "display: block;">ewq | <a href = '#' onclick = "moveChoiceTo(this, -1);">&uarr;</a><a href = '#' onclick = "moveChoiceTo(this, 1);">&darr;</a></span>
            </td>
        </tr>
    </table>
    

    这个JS:

    function moveChoiceTo(elem_choice, direction)
    {
        var curr_index = -1; //index of elem that we should move
        var td = elem_choice.parentElement.parentElement; //link to TD
    
        for (var i = 0; i < td.children.length; i++) //determine index of elem that called this function
            if (td.children[i].children[0] == elem_choice)
            {
                curr_index = i;
                break;
            }
    
        if (curr_index == -1)
            return;
    
        if (curr_index == 0 && direction < 0) //if nowhere to move
            return;
    
        if (curr_index == td.children.length - 1 && direction > 0) //if nowhere to move
            return;
    
        var curr_child = td.children[curr_index]; //save current elem into temp var
        td.children.splice(curr_index, 1); //here I getting exception that splice isn't supported by object, but arent this is array?
        td.children.splice(curr_index + direction, 0, curr_child); //attempt to insert it
    }
    

    我有个例外 splice 不支持,但这应该是一个数组并支持此方法? 我还有什么办法改变孩子的秩序?

    3 回复  |  直到 10 年前
        1
  •  16
  •   dfsq    10 年前

    我将用更简单(更好)的方法补充答案:

    function moveChoiceTo(elem_choice, direction) {
    
        var span = elem_choice.parentNode,
            td = span.parentNode;
    
        if (direction === -1 && span.previousElementSibling) {
            td.insertBefore(span, span.previousElementSibling);
        } else if (direction === 1 && span.nextElementSibling) {
            td.insertBefore(span, span.nextElementSibling.nextElementSibling)
        }
    }
    

    关键思想是使用 insertBefore 方法正确。您也不需要从DOM中删除任何内容。

    演示: http://jsfiddle.net/dq8a0ttt/

        2
  •  4
  •   Aleksandar Gajic    10 年前

    HTMLCollection中不支持拼接,您需要使用.removeChild和.insert

    var before = td.children[curr_index + direction];
    var child = td.children[curr_index];
    
    td.removeChild(child);
    td.insertBefore(child, before); //attempt to insert it      
    
        3
  •  0
  •   Atomenergy    3 年前

    等同于 dfsq的 但有一些修改。

    var selected_element;
    
    
    function moveElementTo(selected_element, direction) {
    
        var element_to_move = selected_element,
            td = element_to_move.parentNode;
    
        if (direction === -1 && element_to_move.previousElementSibling) {
            td.insertBefore(element_to_move, element_to_move.previousElementSibling);
        } else if (direction === 1 && element_to_move.nextElementSibling) {
            td.insertBefore(element_to_move, element_to_move.nextElementSibling.nextElementSibling)
        }
    }
    
    
    
    function move(dir){
        if (selected_element != undefined){
            moveElementTo(selected_element,dir);
        }else{
            console.error("No element Selected to move!");
        }
    
    }
    <div >
    
        <div onclick='selected_element = this;'><button onclick='selected_element = this.parentNode;'> Select element </button>Element One <button onclick='move(-1);'>Move_up</button></div>
        <div onclick='selected_element = this;'><button onclick='selected_element = this.parentNode;'> Select element </button>Element Two <button onclick='move(-1);'>Move_up</button></div>
        <div onclick='selected_element = this;'><button onclick='selected_element = this.parentNode;'> Select element </button>Element Three <button onclick='move(-1);'>Move_up</button></div>
    
    
    </div>

    我的场景是需要首先选择一个元素,因为我将它用于一个弹出式对话框,在该对话框中,长按就会显示操作,选择元素按钮只是为了演示。