2015-01-20 55 views
-1

我一直在尝试了解更多关于鼠标事件的信息,如onmouseoveronmouseoutonmousemove,但没有太多信息。看起来这些事件处理程序默认有一个参数,事件是它自己的。我可以传入更多参数到JavaScript中的事件处理函数吗?

element.onmouseover = mouseoverFunction 

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

但是我希望能够传递其他参数到函数中。

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

如何将事件参数和附加参数传递给函数?

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

回答

3

你可以做一个高阶函数:

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

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

我没看那个,但是看到它不是由IE8和下 – McShaman 2015-01-20 20:52:47

+1

寻找一个'addEventListener'填充工具的支持,或使用'onmouseover'属性正如你所做的那样。 – elclanrs 2015-01-20 20:53:54

相关问题