<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> div{ width: 100px; height: 100px; border-radius: 50%; float: left; margin: 10px; font: bold 20px/100px ""; color: white; text-align: center; } .red{ background: #AD4D49; } .blue{ background: #00A7F6; } </style> <script> window.onload=function () { var ohong=document.getElementsByClassName("red"); var olan=document.getElementsByClassName("blue")[0];
去重、随机数: function fn() { for(var i=0;i<ohong.length;i++){ ohong[i].innerText=Math.ceil(Math.random()*33); for(j=0;j<i;j++){ if(ohong[i].innerText==ohong[j].innerText){ i-- } } } } fn() olan.innerText=Math.ceil(Math.random()*16);
冒泡排序: for(var i=0;i<ohong.length;i++){ for(var j=0;j<i;j++){ if(Number(ohong[j].innerText)>Number(ohong[i].innerText)){ var c; c=ohong[i].innerText; ohong[i].innerText=ohong[j].innerText; ohong[j].innerText=c; } } } } </script> </head> <body> <div class="red"></div> <div class="red"></div> <div class="red"></div> <div class="red"></div> <div class="red"></div> <div class="red"></div> <div class="blue"></div> </body> </html>