JavaScript moving element in the DOM

后端 未结 7 1686
囚心锁ツ
囚心锁ツ 2020-11-27 12:25

Let\'s say I have three

elements on a page. How can I swap positions of the first and third
? jQuery is fine.

7条回答
  •  天涯浪人
    2020-11-27 12:57

    .before and .after

    Use modern vanilla JS! Way better/cleaner than previously. No need to reference a parent.

    const div1 = document.getElementById("div1");
    const div2 = document.getElementById("div2");
    const div3 = document.getElementById("div3");
    
    div2.after(div1);
    div2.before(div3);
    

    Browser Support - 95% Global as of Oct '20

提交回复
热议问题