<! DOCTYPE HTML > < HTML > < head > < Meta charset = "UTF-. 8" > < title > novice tutorial (runoob.com) </ title > </ head > < body > < P > functions can access the internal functions variable definitions: </ the p- > < the Button of the type = "the Button" onclick = "myFunction ()" > point I </ the Button > <p id="demo"></p> <script> function myFunction() { var a = 4; document.getElementById("demo").innerHTML = a * a; } </script> </body> </html>
<! DOCTYPE HTML > < HTML > < head > < Meta charset = "UTF-. 8" > < title > novice tutorial (runoob.com) </ title > </ head > < body > < P > function can be defined in our variable outside the function: </ the p- > < the Button of the type = "the Button" onclick = "myFunction ()" > point I </ the Button > <p id="demo"></p> <script> var a = 4; function myFunction() { document.getElementById("demo").innerHTML = a * a; } </script> </body> </html>
<! DOCTYPE HTML > < HTML > < head > < Meta charset = "UTF-. 8" > < title > novice tutorial (runoob.com) </ title > </ head > < body > < P > global variable count. </ P > < Button type = "Button" the onclick = "myFunction ()" > Count! </ Button > < P ID = "p> <script> var counter = 0; function add() { return counter += 1; } function myFunction(){ document.getElementById("demo").innerHTML = add(); } </script> </body> </html>
<! DOCTYPE HTML > < HTML > < head > < Meta charset = "UTF-. 8" > < title > novice tutorial (runoob.com) </ title > </ head > < body > < P > local variable count. </ P > < Button type = "Button" the onclick = "myFunction ()" > Count! </ Button > < P ID = "p> <script> function add() { var counter = 0; return counter += 1; } function myFunction(){ document.getElementById("demo").innerHTML = add(); } </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <p>局部变量计数。</p> <p id="demo">0</p> <script> document.getElementById("demo").innerHTML = add(); function add() { var counter = 0; function plus() {counter += 1;} plus(); return counter; } </script> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>菜鸟教程(runoob.com)</title> </head> <body> <p>局部变量计数。</p> <button type="button" onclick="myFunction()">计数!</button> <p id="demo">0</p> <script> var add = (function () { var counter = 0; return function () {return counter += 1;} })(); function myFunction(){ document.getElementById("demo").innerHTML = add(); } </script> </body> </html>