码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript函数

时间:2016-09-27 14:43:24      阅读:116      评论:0      收藏:0      [点我收藏+]

标签:

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="按钮" 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函数

标签:

原文地址:http://www.cnblogs.com/Yimi/p/5912423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!