Js学习代码

利用Js对输入的姓名排序

sort.js

var numnames = 0;
var names =  new Array();
function SortNames(){
	// get the name from the text field
	thename = document.theform.newname.value;
	//add the name to the array
	names[numnames] = thename;
	//Increment the counter
	numnames++;
	// sort the array
	names.sort();
	document.theform.sorted.value=names.join("\n");
}

 sort.htm

<HTML>
 <HEAD>
  <TITLE> Array Sorting </TITLE>
  <script type="text/javascript" language= "javascript" src="sort.js">
  </script>
  
 </HEAD>

 <BODY>
 <p> Enter two or more name in the  textarea below.</p>
 <form name="theform">
 Name:
 <input type="text" name = "newname" size="20">
 <input type ="button" name="addname" value="Add" οnclick="SortNames();"> 
 <br>
 <h2>Sorted names</h2>
 <textarea cols="60" rows="10" name ="sorted">
 the sorted names will appear here!
 </textarea>
 <form>
 </BODY>
</HTML>

 

利用Js显示时间

timegreet.js

 

//get the current date
now = new Date();
//split into hours minutes seconds
hours = now.getHours();
mins = now .getMinutes();
secs = now.getSeconds();

//Display the time
document.write("<h2>");
document.write("hours: "+hours+"  mins: "+mins+"  secs: "+secs);
document.write("</h2>");
// display a greeting 
document.write("<p>");
if(hours<10) document.write("Good morning!");
else if(hours>=14&&hours<=17) document.write("Good afternoon!");
else if(hours>17) document.write("Good evening!");
else document.write("Good day!");
document.write("</p>");

 

date.htm

<HTML>
 <HEAD>
  <TITLE> Control Test </TITLE>
 
 </HEAD>

 <BODY>
  <h1>Current Date and Time</h1>
  <p>
  <script language="javascript" type="text/javascript" src="timegreet.js">
  </script>

  </p>
 </BODY>
</HTML>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值