源代码
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>享学课堂</title> </head> <body> <p>单击按钮将数组按升序排序.</p> <button onclick="myFunction()">试试</button> <p id="demo"></p> <script> var points = [40, 100, 1, 5, 25, 10]; document.getElementById("demo").innerHTML = points; function myFunction() { points.sort(function(a, b){return a - b}); document.getElementById("demo").innerHTML = points; } </script> </body> </html>
运行结果