数组排序
view plain copy to clipboard print ?
- <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>
<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 ?
- <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>
<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 ?
- <script language="JavaScript">
- <!--
- window.alert("Hello");
-
- </script>
<script language="JavaScript"> <!-- window.alert("Hello"); // --> </script>
弹出确认框
view plain copy to clipboard print ?
- <script language="JavaScript">
- <!--
- var result = window.confirm("Click OK to continue");
-
- </script>
<script language="JavaScript"> <!-- var result = window.confirm("Click OK to continue"); // --> </script>
定义函数
view plain copy to clipboard print ?
- <script language="JavaScript">
- <!--
- function multiple(number1,number2) {
- var result = number1 * number2;
- return result;
- }
-
- </script>
<script language="JavaScript"> <!-- function multiple(number1,number2) { var result = number1 * number2; return result; } // --> </script>
调用JS函数
view plain copy to clipboard print ?
- <a href="#" onClick="functionName()">Link text</a>
- <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 ?
- <body onLoad="functionName();">
- Body of the page
- </body>
<body onLoad="functionName();"> Body of the page </body>
条件判断
view plain copy to clipboard print ?
- <script>
- <!--
- var userChoice = window.confirm("Choose OK or Cancel");
- var result = (userChoice == true) ? "OK" : "Cancel";
- document.write(result);
-
- </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 ?
- <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>
<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 ?
- <script>
- <!--
- function hello() {
- window.alert("Hello");
- }
- window.setTimeout("hello()",5000);
-
- </script>
<script> <!-- function hello() { window.alert("Hello"); } window.setTimeout("hello()",5000); // --> </script>
定时执行函数
view plain copy to clipboard print ?
- <script>
- <!--
- function hello() {
- window.alert("Hello");
- window.setTimeout("hello()",5000);
- }
- window.setTimeout("hello()",5000);
-
- </script>
<script> <!-- function hello() { window.alert("Hello"); window.setTimeout("hello()",5000); } window.setTimeout("hello()",5000); // --> </script>
取消定时执行
view plain copy to clipboard print ?
- <script>
- <!--
- function hello() {
- window.alert("Hello");
- }
- var myTimeout = window.setTimeout("hello()",5000);
- window.clearTimeout(myTimeout);
-
- </script>
<script> <!-- function hello() { window.alert("Hello"); } var myTimeout = window.setTimeout("hello()",5000); window.clearTimeout(myTimeout); // --> </script>
在页面卸载时候执行函数
view plain copy to clipboard print ?
- <body onUnload="functionName();">
- Body of the page
- </body>
<body onUnload="functionName();"> Body of the page </body>
原文链接: http://www.dlog.cn/nicholascoder/diary/8477