javascript入门操作

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <title> New Document </title>
  <meta name="Generator" content="EditPlus">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <script type="text/javascript">
	function disp_alert(){
		alert("Äãµã»÷Á˾¯¸æ¿ò£¡");
	}
	function onmouseoverTest(){
		alert("Welcome!");
		//document.write("welcome!");
	}
	function disp_confirm(){
		var con=confirm("Are you sure?");
		if(con)
			//alert("You select sure!");
			document.write("Welcome!");
		else
			alert("You select cancel!");
	}
	function disp_prompt(){
		var name=prompt("ÇëÊäÈëÄãµÄÃû×Ö");
		if(name!=null&&name!=""){
			document.write("Hello,"+name+"!");
		}else{
			alert("ÇëÊäÈëÐÕÃû£¡");
		}
	}
	function product(a,b){
		return(a*b);
	}
	function message(){
		try{
			addalert("Welcome guest!");
		}catch(err){
			txt="¸ÃÍøÒ³ÓдíÎó£¡\nµã»÷È·¶¨¼ÌÐø²é¿´±¾Ò³¡£\nµã»÷È¡Ïû·µ»ØÖ÷Ò³¡£";
			if(!confirm(txt)){
				document.location.href="http://www.7789.com";
			}
		}
	}
	function judge(){
		var number=prompt("Input a number from 0 to 10:","");
		try{
			if(number<0){
				throw "small";
			}else if(number>=0&&number<=10){
				alert("You are right!");
			}else{
				throw "large";
			}
		}catch(err){
			if(err=="small")
				alert("The input is smaller!");
			else if(err=="large")
				alert("The input is larger!");
		}
	}
	function checkTime(i){
		if(i<10){
			i="0"+i;
		}
		return i;
	}
	function startTime(){
		var today=new Date();
		var h=today.getHours();
		var m=today.getMinutes();
		var s=today.getSeconds();
		m=checkTime(m);
		s=checkTime(s);
		document.getElementById("txt").innerHTML=h+":"+m+":"+s;
		t=setTimeout("startTime()",500);
	}
	function getValue(){
		var x=document.getElementById("myHeader");
		alert(x);
		alert(x.innerHTML);//×¢ÒâÆäÖеÄinnerHTMLµÄÊéд£¬·ñÔò»á³ö´íÎó¡£
	}
	function getElements(){
		var x=document.getElementsByName("input");
		alert(x);
		alert(x.length);
		alert(x.innerHTML);
	}
	function getElementsByTagNameTest(){
		var x=document.getElementsByTagName("input");
		alert(x.length);
	}
	function validate_required(field,alertTxt){
		with(field){
			if(value==null||value==""){
				alert(alertTxt);
				return false;
			}else{
				return true;
			}
		}
	}
	function validate_email(field,alertTxt){
		with(field){
			apos=value.indexOf("@");
			dotpos=value.lastIndexOf(".");
			if(apos<1||(dotapos-apos)<2){
				alert(alertTxt);
				return false;
			}else{
				return true;
			}
		}
	}
	function validate_form(thisForm){
		with(thisForm){
			if(validate_required(email,"Email must be filled out!")==false){
				email.focus();
				return false;
			}
			if(validate_email(email,"Not a validatde email address!")==false){
				email.focus();
                return false;
			}
		}
	}
  </script>
 </head>

 <body onload="startTime()">
	<script type="text/javascript">
		document.write("Hello World!");
		var x=5+5;
		document.write("<br/>"+x);
		var x=5+"5";
		document.write("<br>"+x);
		var x="5"+5;
		document.write("<br>"+x);
		var x="5"+"5";
		document.write("<br>"+x);
		var x=5;
		document.write("<br>"+(x==="5"));//×¢ÒâÒª¼Ó()£¬·ñÔò²»°´¹æÔòÀ´
		document.write("<br>"+x);
		document.write("<br>"+(x===5));//×¢ÒâÒª¼Ó()£¬·ñÔò²»°´¹æÔòÀ´£¬´ð°¸Îªfalse

		var x=10;
		var result=(x>9)?"Too young!":"Too old!";
		document.write("<br>"+result+"<br>");

		switch(x){
			case 11:
				document.write("10");
				break;
			case 15:
				document.write("15");
				break;
			case 6:
				document.write("6");
				break;
			case 0:
				document.write("0");
				break;
			default:
				document.write("Nothing");
		}

		document.write("<br>2*3="+product(2,3)+"<br>");

		for(i=0;i<10;i++){
			document.write("<h"+i+">±êÌâ"+i+"</h"+i+"><br>");
		}

		var array=new Array("1","2","3","4","5");
		for(x in array){
			document.write(array[x]+"<br>");
		}

		var txt="Hello,\"John\",sdfgdf";
		document.write(txt);
		document.write("<br>You \& me are singing!");
		document.write("<br>Hello \
		everyone!<br>");

		//¶ÔÏó
		var str="Hello world!";
		document.write(str.length+"<br>");//ÕâÀïµÄµ¥´ÊÊý²»°üÀ¨¿Õ¸ñ
		document.write(str.toUpperCase()+"<br>");
		document.write(str.toLowerCase()+"<br>");
		document.write(str.replace("!","!!!"));
		//ʱ¼ä
		document.write(Date());
		//Êý×é
		var array1=new Array("0","1","2","3","4","5");
		var array2=new Array("h","f","a","b","c","d","e");
		document.write("<br>"+array1);
		document.write("<br>"+array1.concat(array2));
		document.write("<br>"+array1);
		document.write("<br>"+array1.join("."));
		document.write("<br>"+array2.sort());

		//boolean
		var b=new Boolean();//×¢ÒâÇ°ÃæµÄÉùÃ÷Ϊvar£¬¶ø²»ÊÇBoolean¡£
		document.write(b);
		var b=new Boolean(0);
		document.write(b);
		var b=new Boolean(false);
		document.write(b);
		var b=new Boolean(true);
		document.write(b);
		var b=new Boolean("false");
		document.write(b);

		//math
		document.write("<br>"+Math.random());
		document.write("<br>"+Math.round(5.2));
		document.write("<br>"+Math.floor(Math.random()*12));
		
		//ÕýÔò±í´ïʽ
		re=new RegExp("ex");
		document.write("<br>"+re.test("Hello every one!"));
		document.write("<br>"+re.test("expression"));
		document.write("<br>"+re.exec("Hello world! expression"));
		re=new RegExp("e","g");
		do{
		    result=re.exec("The best thing in the life is free!");
			document.write(result);
		}while(result!=null)

		var re=new RegExp("e");
		document.write(re.exec("The best thing in the life is free!"));
		re.compile("g");
		document.write(re.test("The best thing in the life is free!"));
		
		document.writeln("<br>"+document.referrer);
		document.writeln("<br>"+navigator.cupClass);
		document.writeln("<br>"+location.port);
		
	</script>
	<div id="txt"></div>
	<br/>
	<input type="button" onclick="disp_alert()" value="¾¯¸æ¿ò"/><br/>
	<input type="text" onmouseover="onmouseoverTest()"/><br/>
	<p onmouseover="onmouseoverTest()">½«Êó±ê·ÅÉÏÀ´ÊÔһϣ¡</p><br/>
	<a href="" onmouseover="onmouseoverTest()">½«Êó±ê·ÅÉÏÀ´ÊÔһϣ¡</a><br/>
	<input type="button" onclick="disp_confirm()" value="½øÈë"/><br/>
	<input type="button" onclick="disp_prompt()" value="×¢²á"/><br/>
	<a href="http://www.7789.com"><img src="logo.jpg"/></a><br/>
	<a href="http://www.7789.com" onmouseover="onmouseoverTest()"><img src="logo.jpg"/></a><br/>
	<input type="button" onclick="message()" value="Ñ¡Ôñ"/><br/>
	<input type="button" onclick="judge()" value="ÅжÏ"/><br/>
	<p id="txt"></p>
	<h1 id="myHeader" onclick="getValue()">±êÌâ1</h1>
	<input type="text" name="input"/>
	<input type="text" name="input"/>
	<input type="text" name="input"/>
	<input type="button" onclick="getElements()" value="getElementsByName"/>
	<input type="button" onclick="getElementsByTagNameTest()" value="getElementsByTagName"/>
	<form action="email.php" onsubmit="return validate_form(this)" method="post">
		Email:<input type="text" name="email" size="30"/>
		<input type="submit" value="submit"/>
	</form>
 </body>
</html>

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏扎心了老铁

java优雅的使用elasticsearch api

本文给出一种优雅的拼装elasticsearch查询的方式,可能会使得使用elasticsearch的方式变得优雅起来,使得代码结构很清晰易读。 建立elast...

1.2K7
来自专栏我是攻城师

Fastjson解析嵌套Map例子

4175
来自专栏码匠的流水账

聊聊storm的WindowedBolt

storm-2.0.0/storm-client/src/jvm/org/apache/storm/topology/IWindowedBolt.java

832
来自专栏高爽的专栏

POI读取Excel常见问题

       最近在做一个将excel导入到报表中的功能,使用了POI来实现,发现POI使用有诸多不便之处,先记录下来,以后可能考虑使用Openxml。    ...

2700
来自专栏编舟记

命令式到函数式编程

应用场景:当我们用到 if-elseif-else 的时候,可以考虑使用 Optional 语义。 举例说明:

782
来自专栏葡萄城控件技术团队

深入浅出OOP(五): C#访问修饰符(Public/Private/Protected/Internal/Sealed/Constants)

访问修饰符(或者叫访问控制符)是面向对象语言的特性之一,用于对类、类成员函数、类成员变量进行访问控制。同时,访问控制符也是语法保留关键字,用于封装组件。 Pub...

3289
来自专栏架构说

[LeetCode] Symmetric Tree

1、题目名称 Symmetric Tree https://leetcode.com/problems/symmetric-tree/ 2、题目内容 Give...

3399
来自专栏高性能服务器开发

(三)dict哈希结构3

/* This function performs just a step of rehashing, and only if there are * no...

2818
来自专栏

Cassandra API60 Java 代码示例

文档地址 http://wiki.apache.org/cassandra/API06,实现了绝大部分示例

1964
来自专栏行者悟空

基于Scala Acotor实现多线程单词统计(WordCount)

2832

扫码关注云+社区

领取腾讯云代金券