JavaScript基础常用语法

数组排序

view plain copy to clipboard print ?
  1. <script language="JavaScript">   
  2. <!--   
  3. var myArray = new Array(5);   
  4. myArray[0] = "z";   
  5. myArray[1] = "c";   
  6. myArray[2] = "d";   
  7. myArray[3] = "a";   
  8. myArray[4] = "q";   
  9. document.write(myArray.sort());   
  10. // -->   
  11. </script>   

<script language="JavaScript"> <!-- var myArray = new Array(5); myArray[0] = "z"; myArray[1] = "c"; myArray[2] = "d"; myArray[3] = "a"; myArray[4] = "q"; document.write(myArray.sort()); // --> </script>
分割字符串

view plain copy to clipboard print ?
  1. <script language="JavaScript">   
  2. <!--   
  3. var myVariable = "a,b,c,d";   
  4. var stringArray = myVariable.split(",");   
  5. document.write(stringArray[0]);   
  6. document.write(stringArray[1]);   
  7. document.write(stringArray[2]);   
  8. document.write(stringArray[3]);   
  9. // -->   
  10. </script>  

<script language="JavaScript"> <!-- var myVariable = "a,b,c,d"; var stringArray = myVariable.split(","); document.write(stringArray[0]); document.write(stringArray[1]); document.write(stringArray[2]); document.write(stringArray[3]); // --> </script>
弹出警告信息

view plain copy to clipboard print ?
  1. <script language="JavaScript">   
  2. <!--   
  3. window.alert("Hello");   
  4. // -->   
  5. </script>  

<script language="JavaScript"> <!-- window.alert("Hello"); // --> </script>
弹出确认框

view plain copy to clipboard print ?
  1. <script language="JavaScript">   
  2. <!--   
  3. var result = window.confirm("Click OK to continue");   
  4. // -->   
  5. </script>  

<script language="JavaScript"> <!-- var result = window.confirm("Click OK to continue"); // --> </script>
定义函数

view plain copy to clipboard print ?
  1. <script language="JavaScript">   
  2. <!--   
  3. function multiple(number1,number2) {    
  4. var result = number1 * number2;   
  5. return result;   
  6. }   
  7. // -->   
  8. </script>  

<script language="JavaScript"> <!-- function multiple(number1,number2) { var result = number1 * number2; return result; } // --> </script>
调用JS函数

view plain copy to clipboard print ?
  1. <a href="#" onClick="functionName()">Link text</a>   
  2. <a href="/"javascript:functionName"()">Link text</a>  

<a href="#" onClick="functionName()">Link text</a> <a href="/"javascript:functionName"()">Link text</a>
在页面加载完成后执行函数

view plain copy to clipboard print ?
  1. <body onLoad="functionName();">  
  2. Body of the page   
  3. </body>   

<body onLoad="functionName();"> Body of the page </body>
条件判断

view plain copy to clipboard print ?
  1. <script>   
  2. <!--   
  3. var userChoice = window.confirm("Choose OK or Cancel");   
  4. var result = (userChoice == true) ? "OK" : "Cancel";   
  5. document.write(result);   
  6. // -->   
  7. </script>  

<script> <!-- var userChoice = window.confirm("Choose OK or Cancel"); var result = (userChoice == true) ? "OK" : "Cancel"; document.write(result); // --> </script>
指定次数循环

view plain copy to clipboard print ?
  1. <script>   
  2. <!--   
  3. var myArray = new Array(3);   
  4. myArray[0] = "Item 0";   
  5. myArray[1] = "Item 1";   
  6. myArray[2] = "Item 2";   
  7. for (i = 0; i < myArray.length; i++) {    
  8. document.write(myArray + "<br/>");   
  9. }   
  10. // -->   
  11. </script>  

<script> <!-- var myArray = new Array(3); myArray[0] = "Item 0"; myArray[1] = "Item 1"; myArray[2] = "Item 2"; for (i = 0; i < myArray.length; i++) { document.write(myArray + "<br/>"); } // --> </script>
设定将来执行

view plain copy to clipboard print ?
  1. <script>   
  2. <!--   
  3. function hello() {    
  4. window.alert("Hello");   
  5. }   
  6. window.setTimeout("hello()",5000);   
  7. // -->   
  8. </script>  

<script> <!-- function hello() { window.alert("Hello"); } window.setTimeout("hello()",5000); // --> </script>
定时执行函数

view plain copy to clipboard print ?
  1. <script>   
  2. <!--   
  3. function hello() {    
  4. window.alert("Hello");   
  5. window.setTimeout("hello()",5000);   
  6. }   
  7. window.setTimeout("hello()",5000);   
  8. // -->   
  9. </script>  

<script> <!-- function hello() { window.alert("Hello"); window.setTimeout("hello()",5000); } window.setTimeout("hello()",5000); // --> </script>
取消定时执行

view plain copy to clipboard print ?
  1. <script>   
  2. <!--   
  3. function hello() {    
  4. window.alert("Hello");   
  5. }   
  6. var myTimeout = window.setTimeout("hello()",5000);   
  7. window.clearTimeout(myTimeout);   
  8. // -->   
  9. </script>  

<script> <!-- function hello() { window.alert("Hello"); } var myTimeout = window.setTimeout("hello()",5000); window.clearTimeout(myTimeout); // --> </script>
在页面卸载时候执行函数

view plain copy to clipboard print ?
  1. <body onUnload="functionName();">  
  2. Body of the page   
  3. </body>  

<body onUnload="functionName();"> Body of the page </body>



原文链接: http://www.dlog.cn/nicholascoder/diary/8477

你可能感兴趣的:(JavaScript基础常用语法)