`
bestxiaok
  • 浏览: 444614 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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" onclick="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>

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics