现在的位置: 首页 > 综合 > 正文

使用prompt输入一句英文句子和排序方式(升/降),将所有单词按排序方式排序后在网页上输出

2018年04月09日 ⁄ 综合 ⁄ 共 1334字 ⁄ 字号 评论关闭
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head> 
  4. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" /> 
  5. <script language="JavaScript" type="text/javascript"> 
  6.  
  7.         function addnew() 
  8.         { 
  9.               c   =   prompt("请输入内容!","");//列   
  10.               way   =   prompt("请输入排序方式(升 1/降 0)!","");//列  
  11.               if   (c   ==   null)    
  12.               {    
  13.                   return false;  
  14.               }    
  15.               else   if   (c.replace(/^\s+|\s+$/g,"")   ==   "")    
  16.               { 
  17.                 alert("输入内容为空!");    
  18.                  return false; 
  19.               } 
  20.             //  alert(c); 
  21.              // alert(way); 
  22.         } 
  23.         addnew(); 
  24.         var arr1 = c.toString().split(" "); 
  25.         if(way==1) 
  26.         { 
  27.             arr1.sort(); 
  28.             for(var i=0;i<arr1.length;i++) 
  29.                 document.write(arr1[i]+"   "); 
  30.         } 
  31.         //(降序) 
  32.         else 
  33.         { 
  34.             arr1.sort(function (a,b){return a>b?-1:1}); 
  35.             for(var i=0;i<arr1.length;i++) 
  36.                 document.write(arr1[i]+"   "); 
  37.         } 
  38.  
  39. </script> 
  40. </body> 
  41.  
  42.  
  43. <title>无标题文档</title> 
  44. </head> 
  45.  
  46. <body> 
  47. </body> 
  48. </html> 

 

本文出自 “阿凡达” 博客,请务必保留此出处http://shamrock.blog.51cto.com/2079212/812996

抱歉!评论已关闭.