标签:style blog http io color ar java sp for
简单的了解了javascript 的基本代码,感觉和c#中的语句差不多。
1 <!DOCTYPE html> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <!--利用var俩声明变量,仅仅声明变量,而不指定具体类型--> 5 <title>javascript study</title> 6 <!-- <script src="commonjs.js" type="text/javascript"></script>--> 7 <script type="text/javascript"> 8 var f1 = function (i1, i2) { return i1 + i2; }//所谓的匿名函数 9 alert(f1(3, 5)); 10 alert(function (x, y) { return x + y; }(2, 4));//生命匿名函数 11 </script> 12 13 <script type="text/javascript"> 14 function person(name,age)//声明的不是类,类似于类 15 { 16 this.Name = name;//里面有动态增加的属性 17 this.Age = age; 18 this.SayHello = function () { alert(this.Name);}//匿名函数 19 } 20 var p = new person("liming", 18); 21 p.SayHello(); 22 alert(p.Age); 23 </script> 24 <script type="text/javascript">//声明函数;注意区别函数和上面类似于类的那种区别 25 function fun(i1, i2) { return i1 + i2; } 26 alert(fun(10,12)); 27 </script> 28 <script type="text/javascript" >//利用数组的地方 29 var names = new Array(); 30 names[0] = "chenlong"; 31 names[1] = "wangxinyu"; 32 names[2] = "godness"; 33 for (var i = 0; i < names.length; i++) 34 alert(names[i]); 35 </script> 36 <script type="text/javascript"> 37 function GetMax(arr) 38 { 39 var max=arr[0]; 40 for(var i=1;i<arr.length;i++) 41 { 42 if (max < arr[i]) max = arr[i]; 43 } 44 return max; 45 46 } 47 var arr = new Array(); 48 arr[0] = 1; 49 arr[1] = 5; 50 arr[2] = 9; 51 arr[3] = 4; 52 arr[4] = 12; 53 arr[5] = 8; 54 arr[6] = 7; 55 alert(GetMax(arr)); 56 </script> 57 <script type="text/javascript"> 58 //练习反转字符串和连接字符串 59 function myreverse(arr)//此处形参属于引用类型,更改后将要影响最后的原数组 60 { 61 for(var i=0;i<arr.length/2;i++) 62 { 63 var temp = arr[i]; 64 arr[i] = arr[arr.length - i - 1]; 65 arr[arr.length - i - 1] = temp; 66 } 67 return arr; 68 } 69 function myjoint(arr) 70 { 71 var str = arr[0]; 72 for(var i=1;i<arr.length;i++) 73 { 74 str += "|" + arr[i]; 75 } 76 return str; 77 } 78 var ar = new Array(); 79 ar[0] = "long"; 80 81 ar[1] = "chen"; 82 myreverse(ar) 83 alert(ar); 84 alert(myjoint(ar)); 85 alert(ar[0]); 86 </script> 87 <script type="text/javascript"> 88 var dict = new Array(); 89 dict["ren"] = "人"; 90 dict["man"] = "男人"; 91 alert(dict.ren); 92 for(var x in dict)//显示的是对象的KEY 93 { 94 alert(x); 95 } 96 var p1 = new Object();//可以动态增加属性 97 p1.Name = "tom"; 98 p1.Age = 10; 99 p1.SayHello = function () { alert("hello"); } 100 for (var e in p1)//可以将一个类的属性取出来 101 alert(e); 102 </script> 103 </head> 104 <body> 105 <input type="button"value="点击"/> 106 <a href="javascript:alert(‘用户你好‘)">点我</a> 107 <a href="www.baidu.com" onclick="alert(‘进入百度网站‘)">百度</a> 108 109 </body> 110 </html>
标签:style blog http io color ar java sp for
原文地址:http://www.cnblogs.com/sytu/p/4084734.html