我一周前开始学习JavaScript,我自己做了一个排序功能。它确实有效,但它需要我修改DOM节点,有没有办法在不改变innerHTML
的情况下对节点进行排序?
<body>
<ul id="list">
<li>Art</li>
<li>Mobile</li>
<li>Education</li>
<li>Games</li>
<li>Magazines</li>
<li>Sports</li>
</ul>
</body>
JavaScript的:
var list = document.getElementById("list");
var myList = list.getElementsByTagName("li");
var a = [];
for (var i = 0; i < myList.length; i++) {
a[i] = myList[i].innerHTML;
}
a.sort();
for (var i = 0; i < myList.length; i++) {
myList[i].innerHTML = a[i];
}
输出:
这很有效,