For novices, if you master the following methods, you can basically make some js yourself, which is a must for getting started
Create script block Hide script code Browse Hello to the non-JavaScript browser. Link to external script file Comment script // This is a comment document .write(“Hello”); // This is a comment /* All of this is a comment */ Output to browser document.write(“Output content ”); Define variables var myVariable = “some value”; characters String addition var myString = “String1” “String2”; String search String replacement thisVar.replace(“Monday”,”Friday”); Format string < ;script language="JavaScript"> Create array Array sorting Split string Pop up warning message Pop-up confirmation box Call JS function < ;a href="#" onClick="functionName()">Link text Link text Execute the function after the page is loaded
Body of the page
Conditional judgment
<script> <br><!-- <br/>var userChoice = window.confirm(“Choose OK or Cancel”); <br/>var result = (userChoice == true) ? “OK” : “Cancel”; <br/>document.write(result); <br/>// --> <br></script>
指定次数循环
<script> <br><!-- <br>var myArray = new Array(3); <br>myArray[0] = “Item 0”; <br>myArray[1] = “Item 1”; <br>myArray[2] = “Item 2”; <br>for (i = 0; i < myArray.length; i ) { <br>document.write(myArray “<br>”); <br>} <br>// --> <br></script>
设定将来执行
<script> <br><!-- <br/>function hello() { <br/>window.alert(“Hello”); <br/>} <br/>window.setTimeout(“hello()”,5000); <br/>// --> <br></script>
定时执行函数
<script> <br><!-- <br/>function hello() { <br/>window.alert(“Hello”); <br/>window.setTimeout(“hello()”,5000); <br/>} <br/>window.setTimeout(“hello()”,5000); <br/>// --> <br></script>
取消定时执行
<script> <br><!-- <br/>function hello() { <br/>window.alert(“Hello”); <br/>} <br/>var myTimeout = window.setTimeout(“hello()”,5000); <br/>window.clearTimeout(myTimeout); <br/>// --> <br></script>
在页面卸载时候执行函数
Body of the page