. 1 <! DOCTYPE HTML > 2 < HTML > . 3 < head > . 4 < Meta charset = "UTF-. 8" > . 5 < title > JavaScript-function What is </ title > . 6 < Script type = "text / JavaScript" > . 7 function name of the function () { 8 function Code 9 } // function Definition syntax 10 </ Script > . 11 </ head > 12 <body> 13 </body> 14 </html>
To avoid repetition code written troubles, we have completed a particular set of statements function called a constructor, and the reusability of code-readability by calling the function;
- Keyword function defined functions
- "Function" you get is a function name
- "Function Code" is replaced with a specific function completion code
- After a good function definition, can not be performed automatically, so the need to call it, just write functions directly in the desired position on the ok
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>JavaScript-什么是函数</title> 6 <script type="text/javascript"> SUMvar13 isADD2 () { function12 is* /. 11 } // Function Definition Syntax 10 Function Code 9 function name of the function () { . 8/ *. 7 = . 3 + 2 ; 14 Alert (SUM); 15 // document.write (SUM); 16 } . 17 ADD2 (); // call the function directly write the name of the function 18 is </ Script > . 19 </ head > 20 is < body > 21 < the INPUT of the type = "the button" name = "" the above mentioned id = "" value = "click" onclick = "ADD2 ()" /> 22 <-! click the button, call the function, onclick to click event -> 23 </body> 24 </html>
Related Development: Web page has a button (the name "click me"), after clicking the button calls the function contxt (), pop-up dialog box "Haha, call the function!."
. 1 <! DOCTYPE the HTML > 2 < HTML > . 3 < head > . 4 < Meta HTTP-equiv = "the Content-the Type" Content = "text / HTML; charset = UTF-. 8" /> . 5 < title > function call </ title > . 6 < Script type = "text / JavaScript" > . 7 contxt () // defined function . 8 { . 9 Alert ( " ha, the calling function! " ); 10 } 11 </script> 12 </head> 13 <body> 14 <form> 15 <input type="button" value="点击我" onclick=" " /> 16 </form> 17 </body> 18 </html>