首页 > 代码库 > 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函数