How to reverse the ordering of list items in an unordered list

前端 未结 8 1025
独厮守ぢ
独厮守ぢ 2020-12-29 17:06

Let\'s say I have an unordered list, like this:

  • Bald Eagle
  • Falcon
  • Condor<
相关标签:
8条回答
  • 2020-12-29 17:20

    Untested, but try:

    var parent = document.getElementById("birds");
    var birdList = parent.getElementsByTagName("li");
    
    for(var i = birdList.length-1; i > 0; --i) {
        parent.appendChild(birdList[i];
    }
    
    0 讨论(0)
  • 2020-12-29 17:24

    An HTMLCollection is only a read-only view of nodes. In order to manipulate nodes, you have to use one of the various DOM manipulation methods. In your case, there happens to be a shortcut; all you need do is to append the child nodes in reverse order.

    var birds = document.getElementById("birds");
    var i = birds.childNodes.length;
    while (i--)
      birds.appendChild(birds.childNodes[i]);
    

    Edit: What's happening is that appendChild removes the node from the document before appending it. This has two effects: a) it saves you from having to make a copy of the list of nodes and removing them before appending them in reverse order, and b) it reverses the order of nodes as it goes, since it appends the last child first and the first child list.

    0 讨论(0)
  • 2020-12-29 17:27

    const list = document.querySelector("ul");
    Array.from(list.children).reverse().forEach(element =>list.appendChild(element));

    0 讨论(0)
  • 2020-12-29 17:32

    Manipulating the birdList doesn't re-order the elements because birdList is just a collection - a NodeList - which is independent of the DOM itself.

    You actually need to get a reference to the <ul id="birds"> element and then use a combination of removeChild(...) and appendChild(...) - maybe insertBefore().

    0 讨论(0)
  • 2020-12-29 17:32

    There's actually no need for JavaScript. If you were to use the semantically correct <ol> (ordered list) element here (since ordering does obviously matter to you), then you can use the reversed attribute to do the exact same thing without any script-dependencies! To reverse the order of the items visually, we'd sprinkle in two lines of CSS with flex-direction:

    ol[reversed] {
      display: flex;
      flex-direction: column-reverse;
    }
    <ol reversed>
      <li>Bald Eagle</li>
      <li>Falcon</li>
      <li>Condor</li>
      <li>Albatross</li>
      <li>Parrot</li>
    </ol>

    0 讨论(0)
  • 2020-12-29 17:40

    You can do it like this (in plain javascript). You can just cycle through the li tags in reverse order and append them to the parent. This will have the natural effect of reversing their order. You get the last one, append it to the end of the list, get the 2nd to last one, append it to the list. Get the 3rd to last one, append it to the end of the list and so on and all the items end up reversed. Note, you can use appendChild on something that is already in the DOM and it will be moved from it's current location automatically (e.g. removed before appended).

    var birds = document.getElementById("birds");
    var birdList = birds.getElementsByTagName("li");
    for (var i = birdList.length - 1; i >= 0; i--) {
        birds.appendChild(birdList[i]);
    }​
    

    Working example: http://jsfiddle.net/jfriend00/GLvxV/

    0 讨论(0)
提交回复
热议问题