<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>冒泡排序</title>
<style type="text/css">
ul{
margin:0;
padding: 0;
}
li{
list-style: none;
color: #0cf;
font-size: 20px;
width: 80px;
text-align: center;
}
.btn{
background: #0cc;
width: 80px;
height: 30px;
line-height: 30px;
text-align: center;
cursor: pointer;
color: #fff;
border-radius: 5px;
}
</style>
</head>
<body>
<div>
<ul>
<li>50</li>
<li>30</li>
<li>45</li>
<li>12.5</li>
<li>11</li>
<li>63</li>
<li>16</li>
<li>5</li>
<li>33</li>
<li>21</li>
</ul>
</div>
<div class="btn">点击排序</div>
<script type="text/javascript">
var lis=document.getElementsByTagName('li');
var arry=[];
var a=0;
var btn=document.querySelector('.btn');
//console.log(btn);
for (var i = 0; i < lis.length; i++) {
// this.index=i;
arry[i]=lis[i].innerHTML;
}
console.log(arry);
btn.οnclick=function(){
sort(arry);
console.log(arry);
for (var i = 0; i < lis.length; i++) {
// this.index=i;
lis[i].innerHTML=arry[i];
}
}
//排序函数
function sort(arr){
for (var i = 0; i < arr.length-1; i++) {
for (var j = 0; j < arr.length-i-1; j++) {
if (parseFloat(arr[j])>parseFloat(arr[j+1])) { //使用parseFloat()函数对字符串进行转带浮点数值再比较
a=arr[j];
arr[j]=arr[j+1];
arr[j+1]=a;
}
}
}
return arr;
}
</script>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>冒泡排序</title>
<style type="text/css">
ul{
margin:0;
padding: 0;
}
li{
list-style: none;
color: #0cf;
font-size: 20px;
width: 80px;
text-align: center;
}
.btn{
background: #0cc;
width: 80px;
height: 30px;
line-height: 30px;
text-align: center;
cursor: pointer;
color: #fff;
border-radius: 5px;
}
</style>
</head>
<body>
<div>
<ul>
<li>50</li>
<li>30</li>
<li>45</li>
<li>12.5</li>
<li>11</li>
<li>63</li>
<li>16</li>
<li>5</li>
<li>33</li>
<li>21</li>
</ul>
</div>
<div class="btn">点击排序</div>
<script type="text/javascript">
var lis=document.getElementsByTagName('li');
var arry=[];
var a=0;
var btn=document.querySelector('.btn');
//console.log(btn);
for (var i = 0; i < lis.length; i++) {
// this.index=i;
arry[i]=lis[i].innerHTML;
}
console.log(arry);
btn.οnclick=function(){
sort(arry);
console.log(arry);
for (var i = 0; i < lis.length; i++) {
// this.index=i;
lis[i].innerHTML=arry[i];
}
}
//排序函数
function sort(arr){
for (var i = 0; i < arr.length-1; i++) {
for (var j = 0; j < arr.length-i-1; j++) {
if (parseFloat(arr[j])>parseFloat(arr[j+1])) { //使用parseFloat()函数对字符串进行转带浮点数值再比较
a=arr[j];
arr[j]=arr[j+1];
arr[j+1]=a;
}
}
}
return arr;
}
</script>
</body>
</html>