1.JavaScript 语句和 JavaScript 变量都对大小写敏感。
2.重新声明 JavaScript 变量
如果重新声明 JavaScript 变量,该变量的值不会丢失:
在以下两条语句执行后,变量 carname 的值依然是 "Volvo":
var carname="Volvo"; var carname; 3.JavaScript 拥有动态类型。这意味着相同的变量可用作不同的类型:javascript数据类型 -----字符串、数字、布尔、数组、对象、Null、Undefined 4.数组:(中括号)
var cars=["Audi","BMW","Volvo"]; 5.JavaScript 对象(大括号)
var person={firstname:"Bill", lastname:"Gates", id:5566};
name=person.lastname; name=person["lastname"]; 6.创建 JavaScript 对象
person=new Object(); person.firstname="Bill"; person.lastname="Gates"; person.age=56; person.eyecolor="blue";7.如果把数字与字符串相加,结果将成为字符串
8.Switch
switch(n) { case 1: 执行代码块 1 break; case 2: 执行代码块 2 break; default: n 与 case 1 和 case 2 不同时执行的代码 }
9.For/In 循环
10.throw exception
异常可以是 JavaScript 字符串、数字、逻辑值或对象。e.g.
<script> function myFunction() { try { var x=document.getElementById("demo").value; if(x=="") throw "empty"; if(isNaN(x)) throw "not a number"; if(x>10) throw "too high"; if(x<5) throw "too low"; } catch(err) { var y=document.getElementById("mess"); y.innerHTML="Error: " + err + "."; } } </script> <h1>My First JavaScript</h1> <p>Please input a number between 5 and 10:</p> <input id="demo" type="text"> <button type="button" onclick="myFunction()">Test Input</button> <p id="mess"></p>
11.E-mail 验证
<html> <head> <script type="text/javascript"> function validate_email(field,alerttxt) { with (field) { apos=value.indexOf("@") dotpos=value.lastIndexOf(".") if (apos<1||dotpos-apos<2) {alert(alerttxt);return false} else {return true} } } function validate_form(thisform) { with (thisform) { if (validate_email(email,"Not a valid e-mail address!")==false) {email.focus();return false} } } </script> </head> <body> <form action="submitpage.htm"onsubmit="return validate_form(this);" method="post"> Email: <input type="text" name="email" size="30"> <input type="submit" value="Submit"> </form> </body> </html>
时间: 2024-10-06 17:32:48