这段js代码具体是什么问题,求助
<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>排序</title><script>wind...
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>排序</title>
<script>
window.onload = function () {
var oBtn = document.getElementById('btn1');
var oUl = document.getElementById('ul1');
oBtn.onclick = function () {
var aLi = oUl.getElementsByTagName('li');
var arr = [];
for ( var i=0; i<aLi.length; i++);{
arr[i] = aLi[i];
}
arr.sort(function (li1,li2) {
var n1 = parseInt(li1.innerHTML);
var n2 = parseInt(li2.innerHTML);
return n1-n2;
});
for (var j=0; j<arr.length; j++) {
oUl.appendChild(arr[j]);
}
}
}
</script>
</head>
<body>
<input id="btn1" type="button" value="排序">
<ul id="ul1">
<li>12</li>
<li>72</li>
<li>114</li>
<li>5552</li>
<li>78</li>
<li>3</li>
</ul>
</body>
</html>
代码运行不了,具体是哪里有问题 展开
<html lang="en">
<head>
<meta charset="UTF-8">
<title>排序</title>
<script>
window.onload = function () {
var oBtn = document.getElementById('btn1');
var oUl = document.getElementById('ul1');
oBtn.onclick = function () {
var aLi = oUl.getElementsByTagName('li');
var arr = [];
for ( var i=0; i<aLi.length; i++);{
arr[i] = aLi[i];
}
arr.sort(function (li1,li2) {
var n1 = parseInt(li1.innerHTML);
var n2 = parseInt(li2.innerHTML);
return n1-n2;
});
for (var j=0; j<arr.length; j++) {
oUl.appendChild(arr[j]);
}
}
}
</script>
</head>
<body>
<input id="btn1" type="button" value="排序">
<ul id="ul1">
<li>12</li>
<li>72</li>
<li>114</li>
<li>5552</li>
<li>78</li>
<li>3</li>
</ul>
</body>
</html>
代码运行不了,具体是哪里有问题 展开
展开全部
appendChild();这个方法只能追加节点对象而不是字符串,比如li节点,排序也不对
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
看了一天才知道你在for()后面加了个分号,太粗心了吧
for ( var i=0; i<aLi.length; i++);{
arr[i] = aLi[i];
}
for ( var i=0; i<aLi.length; i++);{
arr[i] = aLi[i];
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询