Reorder html elements in dom

后端 未结 5 492
眼角桃花
眼角桃花 2020-12-10 15:17

I have a list of elements:

相关标签:
5条回答
  • 2020-12-10 15:49

    no need to copy all items .. twice

    var wrapper = $('.wrapper'), 
        items = wrapper.children('.abc'),
        arr = [2,1,0];
    
    //items.detach(); if arr doesn't reuse all items
    wrapper.append( $.map(arr, function(v){ return items[v] }) );
    
    0 讨论(0)
  • 2020-12-10 15:52

    Keep in mind that when you add an element that is already in the DOM, this element will be moved, not copied.

    CodePen

    let wrapper=document.querySelector(".wrapper");
    let children=wrapper.children;
    let newOrder=[3,2,1,0];
    //it means that the first element you want it to be the four element
    //The second element you want it to be the third
    //the third element you want it to be the second
    //the four element you want it to be the first
    for(let i=0;i<newOrder.length;i++){
      for(let j=0;j<newOrder.length;j++){
        if(i==newOrder[j]){
          wrapper.appendChild(children[j]);
          break;
        }
      }
    }
    <div class="wrapper">
      <div>a</div>
      <div>b</div>
      <div>c</div>
      <div>d</div>
    </div>

    0 讨论(0)
  • 2020-12-10 16:03

    I know this is an old question, but google lead me to it. There is a sub property on flexbox (css) called 'order', that allow you to choose the order that elements are displayed. Is possible to use javascript to change this sub property and reorder the displayed elements.

    https://www.w3schools.com/cssref/css3_pr_order.asp

    Edit 1 - code example:

    <style>
     .wrapper {
       display: flex;
       flex-flow: column;
     }
    </style>
    
    <div class="wrapper">
      <div class="abc">One</div>
      <div class="abc">Tow</div>
      <div class="abc">Three</div>
    </div>
    <button id="reorder" type="button">Reorder</button>
    <button id="unreorder" type="button">Unreorder</button>
    
    <script>
    var reorderButton = document.querySelector('#reorder');
    var unreorderButton = document.querySelector('#unreorder');
    var abcDivs = document.querySelectorAll('.abc');
    var newOrder = [2, 1, 0];
    
    reorderButton.addEventListener('click', function() {
      abcDivs.forEach(function(element, index) {
        element.style.order = newOrder[index];
      });
    });
    unreorderButton.addEventListener('click', function() {
      abcDivs.forEach(function(element, index) {
        element.style.order = null;
      });
    });
    </script>
    
    0 讨论(0)
  • 2020-12-10 16:09

    This solution works better for me, we build an array of elements ([...wrapper.children]) then we use .sort based on a model ([5,4,3,2,1,0]) and then we use appendChild. Since the elements are the same as the originals, the event listeners remain working fine. Here the code:

    //this is just to add event listeners
    [...document.getElementsByClassName("abc")].forEach(e =>
      e.addEventListener("click", ev => console.log(ev.target.innerText))
    );
    
    var wrapper = document.getElementsByClassName("wrapper")[0];
    var items = [...wrapper.children];
    
    const newOrder = [5,4,3,2,1,0]
    
    items.sort((a, b)=>newOrder.indexOf(items.indexOf(a)) - newOrder.indexOf(items.indexOf(b)));
    
    items.forEach(it=>wrapper.appendChild(it));
    <div class="wrapper">
      <div class="abc">0</div>
      <div class="abc">1</div>
      <div class="abc">2</div>
      <div class="abc">3</div>
      <div class="abc">4</div>
      <div class="abc">5</div>
    </div>

    As you can see, if you click on 0, 1, etc, the event listener works.

    0 讨论(0)
  • 2020-12-10 16:13

    If you want a pure Javascript solution (no jQuery involved)

    var arr = [2,1,0];
    var wrapper = document.getElementsByClassName("wrapper");
    var items = wrapper[0].children;
    var elements = document.createDocumentFragment();
    
    arr.forEach(function(idx) {
        elements.appendChild(items[idx].cloneNode(true));
    });
    
    wrapper[0].innerHTML = null;
    wrapper[0].appendChild(elements);
    

    A little improvement of my previous answer. Fiddle: https://jsfiddle.net/jltorresm/1ukhzbg2/2/

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