JS函数

作者: 趁年轻拼一拼 | 来源:发表于2018-06-06 18:26 被阅读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('点击可执行的函数');

    相关文章

      网友评论

        本文标题:JS函数

        本文链接:https://www.haomeiwen.com/subject/rhlisftx.html