以下是代码:
index.html <!DOCTYPE html> <html> <head> <title>Sorting</title> <link rel="stylesheet" type="text/css" href="style.css"> </head> <body> <!--主要页面结构--> <div class="container"> <input type="text" name="number" id="number" placeholder="Please enter 10 numbers(don't leave space)" /> <a href="javascript:void()" class="sortbtn" id="resultBtn">Sort</a> <label class="title">After Sorted:</label> <!--以下三个label分别显示冒泡,插入,快速排序的结果--> <label class="result" for="bubblesort"></label> <label class="result" for="insertsort"></label> <label class="result" for="quicksort"></label> </div> <!--end--> <script type="text/javascript" src="script.js"></script> </body> </html>
来给这个页面写点样式,不然就太不好看了。
style.css *{ margin: 0; padding: 0; list-style: none; } .container{ width: 400px; margin: 100px auto; } input[type="text"]{ display: block; width: 400px; height: 40px; text-align: center; line-height: 40px; outline: none; font-size: 14px; border-radius: 15px; border: 1px solid #aaaaaa; } .sortbtn{ display: block; width: 200px; height: 34px; text-align: center; line-height: 34px; border: 1px solid black; border-radius: 10px; text-decoration: none; color: black; margin-left: 100px; margin-top: 30px; } .sortbtn:hover{ display: block; background-color: black; color: #ffffff; } label{ display: block; width: 200px; text-align: center; margin-left: 100px; margin-top: 20px; font-size: 20px; }
然后就是主要的功能实现了。
script.js window.onload = function(){ var btn = document.getElementById("resultBtn"); //结果
最后的效果是这样的:
未输入情况下,一只安静的文本框,一只安静的按钮和一个label:
输出" width="600" height="333"/>
输入的不是数字,未输入十位或者超出十位,或者为空,点击按钮之后,都会提示错误:
为空:
输出" width="600" height="391"/>
不是数字且不足十位:
输出" width="600" height="360"/>
超出十位:
输出" width="600" height="386"/>
输入正确的情况下:
输出" width="600" height="538"/>
提示:输入的数字之间不用加空格,输入的数字之间不用加空格,输入的数字之间不用加空格,重要的事情说三遍
需要注意的是文本框中输入的数字只能是一位的数字(0-9),有关两位数字甚至更多位数字的排序方法请继续追踪此网站。希望这些内容对大家有所帮助。
更多JS实现冒泡排序,插入排序和快速排序并排序输出相关文章请关注PHP中文网!