我爱编程

JS函数

2018-06-06  本文已影响0人  趁年轻拼一拼

函数:预先定义好的,可以被反复利用的代码

函数不调用不执行

格式:function    fn(){//代码}

fn表示函数名

例:<button>按钮</button>

①function  fn(){

    console.log('hello  js');

fn();

②点击事件:onclick

<button onclick='fn'()>按钮</button>

function  fn(){

    console.log('hello  js');

带参数的函数

格式:function    add(a,b){//参数  形参: 形式上的参数

    alert(a+b);

add(a的值,b的值);//实参:实际上的参数

例:<button>带参数的函数</button>

①function  add(a,b){

    alert(a+b);

add(3,5);

②点击事件:onclick

<button onclick='add(6,4)'>带参数的函数</button>

function  add(a,b){

    alert(a+b);

例:点击可执行的函数

①<button  id='btn'></button>

    var  btn=document.getElementById('btn');

      btn.onclick=function(){

      alert('点击可执行的函数');

②点击事件:onclick

<button onclick='aa'()>按钮</button>

    function  aa(){

      console.log('点击可执行的函数');

上一篇 下一篇

猜你喜欢

热点阅读