首页 > 代码库 > JavaScript函数
JavaScript函数
Function 函数名(){
函数体;(代码块)
}
定义一个加法函数
<html> <head> <meta charset="utf-8 "> <meta name="generator" content="HTML Tidy for HTML5 (experimental) for Windows https://github.com/w3c/tidy-html5/tree/c63cc39" /> <title></title> </head> <body> <script>/* var a1=10; var a2=20; var sum=a1+a2; alert(sum);*/ function demo(a,b) { var sum=a+b; return sum; } var v1=demo(10,10); var v2=demo(20,10); var v3=demo(40,10); var v4=demo(60,10); alert(v1); alert(v2); alert(v3); alert(v4); </script> </body></html>
带按钮的加法
<html> <head> <meta charset="utf-8" /> <meta name="generator" content="HTML Tidy for HTML5 (experimental) for Windows https://github.com/w3c/tidy-html5/tree/c63cc39" /> <title></title> </head> <body> <script>function demo(){var a=10;var b=20;var sum=a+b;alert(sum);}</script><!--<form><input type="button" value="http://www.mamicode.com/按钮" onclick="demo()"/></form>--><button onclick="demo()">按钮</button> </body></html>
带参函数
<html> <head> <meta charset="utf-8" /> <meta name="generator" content="HTML Tidy for HTML5 (experimental) for Windows https://github.com/w3c/tidy-html5/tree/c63cc39" /> <title>函数</title> </head> <body> <script> function demo(a,b){ var sum=a+b; alert(sum);} demo(100,200); demo(10,20);</script> </body></html>
两个按钮
<html> <head> <meta charset="utf-8" /> <meta name="generator" content="HTML Tidy for HTML5 (experimental) for Windows https://github.com/w3c/tidy-html5/tree/c63cc39" /> <title>函数</title> </head> <body> <script>function demo(name,age){alert("hello"+name+",我的年龄是:"+age);}</script><button onclick="demo(‘iwen‘,20)">按钮</button><button onclick="demo(‘iannn‘,25)">按钮</button> </body></html>
JavaScript函数
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。