The realization method of using AppendChild to sort <li> label _javascript skill

Source: Internet
Author: User

Sort by from big to small

AppendChild:

Assuming that child Node B is already in parent a, then A.appendchild (b) is: 1. First remove child Node B from parent a, 2. Then add child node B to a, and put it at the very end.

<body>
  <button id= "BT1" > Submit </button>
  <ul id= "UL1" > 
    <li>32</li>
    <li>243</li>
    <li>43</li>
    <li>24</li>
    <li id= "T" >2 </li>
    <li>84</li>
    <li>84</li>
    <li>25</li>
  </ Ul>
</body>
<script>
  window.onload=function () {
  var Oul1=document.getelementbyid (' Ul1 ');
  var Obt=document.getelementbyid (' bt1 ');

  Obt.onclick=function () {
  var oli=document.getelementsbytagname (' Li ');

  var arr=[];
  Put <li> tags in an empty arr array for
  (Var i=0;i<oli.length;i++) {
    arr[i]=oli[i]; 
     }
  Sort, each element in the array is a <li>, so you want to use innerHTML
  arr.sort (function (li1,li2) {
    var n1=parseint ( li1.innerhtml);
    var n2=parseint (li2.innerhtml);  
    return n1-n2; 
  })
  Sort by AppendChild for
  (var i=0;i<arr.length;i++) {
    oul1.appendchild (arr[i]);
}} </script>

The above is small series for everyone to bring the JS using AppendChild to the label to sort the implementation of the whole content, I hope that we support cloud Habitat Community ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.