我可以在Javascript中传递更多的参数到事件处理函数吗? [英] Can I pass more arguments into event handler functions in Javascript?

查看:106
本文介绍了我可以在Javascript中传递更多的参数到事件处理函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在试图找出有关鼠标事件的更多信息,例如 onmouseover onmouseout onmousemove 但没有太多的信息。似乎这些事件处理程序默认情况下有一个参数,事件是自己的。

I have been trying to find out more about mouse events such as onmouseover, onmouseout and onmousemove but there is not much info. It seems these event handlers by default have a single argument, the event its self.

element.onmouseover = mouseoverFunction

function mouseoverFunction( event ) {
    // Do stuff here
}

然而,我想要将其他参数传递到函数中。

However I want to be able to pass other arguments to into the function.

function mouseoverFunction( event, moreArgs ) {
    // Do stuff here
}

如何传递事件参数和函数的其他参数?

How do I pass the event argument and additional arguments to the function?

还可以将更多的参数传递给事件处理函数吗?

Also is it ok to pass more arguments into an event handler function?

推荐答案

你可以做一个更高阶的函数:

You can make a higher-order function:

function mouseover(arg1, arg2) {
  return function(event) {
    // Do stuff here
  }
}

element.addEventListener('mouseover', mouseover(1, 2))

这篇关于我可以在Javascript中传递更多的参数到事件处理函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆