简体   繁体   English

任何人都可以帮助我如何根据时间对 div 进行排序吗? js

[英]Can anyone help me on how to sort div according to time? js

I want to sort these html according to the date and time inside the div .inner h6 .我想根据 div .inner h6的日期和时间对这些 html 进行排序。 I tried many javascript and couldn't make it.我尝试了很多 javascript 并不能成功。 I am new to js , please help我是 js 新手,请帮忙

<div class="root-history">
    <div class="inner sc">
        <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galle</span><br>
        <h6>7/13/2021 8:10:20 PM</h6>
    </div>

 
    <div class="inner sc">
        <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galle</span><br>
        <h6>7/13/2021 8:11:55 PM</h6>
   </div>
 
    <div class="inner sc">
        <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galle</span><br>
        <h6>7/13/2021 8:10:12 PM</h6>
    </div>
 
    <div class="inner sc">
        <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galle</span><br>
        <h6>7/13/2021 8:10:43 PM</h6>
    </div>
 
    <div class="inner sc">
        <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galle</span><br>
        <h6>7/13/2021 8:10:34 PM</h6>
    </div>
</div>

Something like this?像这样的东西?

  • We are sorting the children via new Date(a.querySelector("h6").innerText) - new Date(b.querySelector("h6").innerText)我们通过new Date(a.querySelector("h6").innerText) - new Date(b.querySelector("h6").innerText)
  • We are re-adding the children in corresponding order我们正在按照相应的顺序重新添加孩子

 const root = document.getElementById("root"); [...root.querySelectorAll(".inner")] .sort((a, b) => (new Date(a.querySelector("h6").innerText) - new Date(b.querySelector("h6").innerText))).forEach(e => root.appendChild(e));
 <div class="root-history" id="root"> <div class="inner sc"> <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galle</span><br> <h6>7/13/2021 08:10:20 PM</h6> </div> <div class="inner sc"> <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galle</span><br> <h6>7/13/2021 08:11:55 PM</h6> </div> <div class="inner sc"> <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galle</span><br> <h6>7/13/2021 08:10:12 PM</h6> </div> <div class="inner sc"> <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galle</span><br> <h6>7/13/2021 08:10:43 PM</h6> </div> <div class="inner sc"> <span>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galle</span><br> <h6>7/13/2021 08:10:34 PM</h6> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM