事件处理程序的参数与传参

先给个例子。

访问事件对象
<body>
    <button onclick="sayHi()">sayHi</button>
    <button class="btn">bless</button>
    <script>
    function sayHi(){
        console.log(window.event === event);
        console.log(event);
        console.log("hello");
    }
    var btn = document.querySelector(".btn");
    btn.onclick = function(e){
        console.log(e);
        console.log("have a nice day");
    }
    </script>
</body>

在这里插入图片描述
<button onclick="sayHi()">sayHi</button>,事件对象eventwindow上,因此 通过window.event访问。
btn.onclick = function(e){},事件处理函数的第一个参数是事件对象。当然,也可以通过因此 通过window.event访问事件对象。
想起 跨浏览器的事件处理程序 写法。瞧,不论onclick在html里还是js里,getEvent都能够返回 事件对象。

<body>
    <button class="btn">bless</button>
    <script>
    var btn = document.querySelector(".btn");
    var EventUtil = {
        addHandler:function(elm,type,handler){
            if(elm.addEventListener){
                elm.addEventListener(type,handler,false);
            }else
             if(elm.attachEvent){
                elm.attachEvent("on"+type,handler);            
            }else{
                elm["on"+type] = handler;
            }
        },
        getEvent:function(event){
            return event||window.event;
        }
    }
    EventUtil.addHandler(btn,"click",function(e){
        var event = EventUtil.getEvent(e);
        console.log(event);
        console.log("have a nice day");
    })
    </script>
</body>
向事件处理程序传参
匿名函数 赋给 onclick

事件处理程序是一个匿名函数,它的 第一个参数 是 事件对象,只有这一个参数。

<body>
    <button >sayHi</button>
    <script>
    var b = document.querySelector("button");
    b.onclick = function(e){
        console.log(e.type);
        console.log("hello world");
    }
    </script>
</body>
已声明函数 赋给 onclick

事件处理程序 是 一个 已声明函数 doSomething,它的第一个参数 是 事件对象,只有这一个参数。

<body>
    <button >sayHi</button>
    <script>
    var b = document.querySelector("button");
    function doSomething(e){
        console.log(e.type);
        console.log("hello world");
    }
    b.onclick = doSomething;
</body>

不论是将 匿名函数 还是 已声明函数 赋给 onclick事件处理程序是一个函数,且只有一个参数,那就是 事件对象。

如果想向事件处理程序中传入其他参数,最简单直接的方式就是在 事件处理程序 中 通过调用其他函数,从而传入额外参数。就像这样。

<body>
    <button >sayHi</button>
    <script>
    function doSomething(message,e){
        console.log(`${e.type} occured,and the message is ${message}`)
    }
    var button = document.querySelector("button");
    button.onclick = function(e){
        doSomething("hello world",e);
    }
  </script>
</body>

我们来研究一个稍复杂点儿的,和this指向有关。
向 事件处理程序 传入额外参数 有三种方式。

  • 第一种,就像刚刚那样,在 事件处理程序 中 直接调用TODO.doSomething方法,并传入参数name:Nicholas和 事件对象e
<body>
    <button>click me </button>
    <script>
    var TODO = {
        message:"hello world",
        doSomething:function(name,e){
            console.log(`${e.type} occured,and ${name} send a message:${this.message}`);
        }
    }

    var button = document.querySelector("button");
    button.onclick = function(e){
        TODO.doSomething("Nicholas",e);
    }
  </script>
  • 第二种,使用Function.prototype.bind
<body>
    <button>click me </button>
    <script>
    var TODO = {
        message:"hello world",
        doSomething:function(name,e){
            console.log(`${e.type} occured,and ${name} send a message:${this.message}`);
        }
    }

    var button = document.querySelector("button");
    button.onclick = TODO.doSomething.bind(TODO,"Nicholas")
   
  </script>
</body>
  • 第三种,那就是自定义bind,本质上和Function.prototype.bind是一样的
<body>
    <button>click me </button>
    <script>
    var TODO = {
        message:"hello world",
        doSomething:function(name,e){
            console.log(`${e.type} occured,and ${name} send a message:${this.message}`);
        }
    }

    function bind(fn,context){
        var args = Array.prototype.slice.call(arguments,2);
        return function (){
            var finalArgs = args.concat(Array.prototype.slice.call(arguments));
            fn.apply(context,finalArgs);
            // fn.call(context,...finalArgs);
        }
    }
    var button = document.querySelector("button");
    button.onclick = bind(TODO.doSomething,TODO,"Nicholas");
   
  </script>
</body>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值