javascript的基本语法,javascript具有所有语言都具有的所有程序结构:顺序/分支/循环这三种结构。
if语句的例子:
1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2: <html>
3: <head>
4: <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5: <title>Untitled Document</title>
6: <script type="text/javascript">1:
2: function testIF(){3: var tempTextBox = document.getElementById("number").value;4:
5: if(tempTextBox>10)6: {
7: alert("您输入的数字大于10");8: }
9: else10: {
11: alert("您输入的数字小于10");12: }
13: }
14:
</script>
7: </head>
8: <body>
9: <form id="testform">
10: <input type="text" id="number" value="">
11: <input type="button" value="判断" onclick="testIF()">
12: </form>
13: </body>
14: </html>
for语句的例子:
1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2: <html>
3: <head>
4: <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5: <title>Untitled Document</title>
6: <script type="text/javascript">1:
2: function testFor()3: {
4: var tempValue=document.getElementById("testFor").value;5: for(i=0;i<tempValue;i++)6: {
7: alert(i);
8: }
9: }
10:
</script>
7: </head>
8: <body>
9: <input type="text" id="testFor">
10: <input type="button" id="demoid" value="点击" onclick="testFor()">
11: </body>
12: </html>
在javascript中我们也支持面对对象,只不过是模拟出来的。
1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2: <html>
3: <head>
4: <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
5: <title>Untitled Document</title>
6: <script type="text/javascript">1:
2:
3: var Person=new Object();4: function sum(a,b)5: {
6: return a+b;7: }
8:
9: Person.sum=sum;
10: //sum是一个属性还是一个方法要看后面跟的是一个什么11: alert(Person.sum(3,5));
12: //可以使用匿名函数的方式来实现方法的声明13: Person.age=function(){return 8+8;};14: alert(Person.age());
15: //带参数的匿名函数16: Person.test=function(a,b){return a+b};17: alert(Person.test(4,6));
18:
19: //以下方式是为了模拟属性的实现20: Person.Age=20;
21: Person.setAge=function(a){Person.Age=a};22: Person.setAge(555);
23: alert(Person.Age);
24:
</script>
7: </head>
8: <body >
9: </body>
10: </html>