Using Array.prototype.sort.call to sort a HTMLCollection

后端 未结 3 1338
闹比i
闹比i 2020-12-17 05:05
var down=function(a,b){alert(a)}
Array.prototype.sort.call(table.tBodies[0].childNodes,down)
Array.prototype.sort.call([0,1,2,3],down)

Why do I not

3条回答
  •  离开以前
    2020-12-17 05:27

    I correct the code of gaetanoM, this one works with IE :

    window.onload = function() {
      var parentNode = document.getElementById('test-list');
      var e = document.getElementById('test-list').children;
      [].slice.call(e).sort(function(a, b) {
      	if (a.textContent > b.textContent) return 1;
      if (a.textContent < b.textContent) return -1;
      return 0;
       }).forEach(function(val) {
        parentNode.appendChild(val);
      });
    }
    1. Scheme
    2. JavaScript
    3. Python
    4. Ruby
    5. Haskell
    6. Tata
    7. Aaaaa
    8. Drue

提交回复
热议问题