我换成BUTTON 哪里出错了 运行不了

来源:7-22 数组排序sort()

慕移动1741139

2017-08-15 09:55

<!DOCTYPE html>
<html>
<head>
   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
   <title>Array对象 </title>
   <script type="text/javascript">
       function sortNum(a,b) {
           var myarr = new Array("80","16","50","6","100","1");
           return b - a;
           var soult = myarr.sort(sortNum);
       }
       document.getElementById("demo").innerHTML=soult;
   </script>
</head>
<body>
<button onclick="sortNum(a,b)">点我对函数排序</button>
<p id="demo">80,16,50,6,100,1</p>
</body>
</html>

写回答 关注

2回答

  • 慕粉4395516
    2017-08-15 11:09:01
    已采纳

    function sortNum() {

               var soult;

               var myarr = new Array("80","16","50","6","100","1");

               soult = myarr.sort(function compare(a,b){return a -b;});

               document.getElementById("demo").innerHTML=soult;

           }


    慕粉4395... 回复慕移动174...

    啊,没懂你的意思

    2017-08-15 13:54:31

    共 5 条回复 >

  • 慕圣9424722
    2017-08-19 00:47:55

    <!DOCTYPE html>

    <html>

    <head>

       <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

       <title>Array对象 </title>

       <script type="text/javascript">

          function b(){

          var myarr = new Array("80","16","50","6","100","1");

          function sortNum(a,b) {

               return b - a;  //升序

           }

           var soult = myarr.sort(sortNum);

           document.getElementById("demo").innerHTML=soult;

          }

       </script>

    </head>

    <body>

    <button onclick="b()">点我对函数排序</button>

    <p id="demo">80,16,50,6,100,1</p>

    </body>

    </html>


    慕圣9424...

    <script type="text/javascript"> function sortNum() { var soult; var myarr = new Array("80","16","50","6","100","1"); soult = myarr.sort(function(a,b){return a -b;}); document.getElementById("demo").innerHTML=soult; } </script>

    2017-08-19 01:01:44

    共 1 条回复 >

JavaScript进阶篇

本课程从如何插入JS代码开始,带您进入网页动态交互世界

468061 学习 · 21891 问题

查看课程

相似问题