如何将 DOM 事件作为事件处理程序绑定方法传递

How can I pass the DOM event as an event handler bind method

nav.addEventListener('mouseover',handleHover.bind(null,e,0.5))

我想将事件本身作为参数传递给绑定方法,但它给出了一个错误。 e 未定义。如何将事件本身传递给事件处理程序绑定方法?

您可以传递参数:

const handleHover = (event, opacity) => { 
  event.target.style.opacity = opacity; 
}

nav.addEventListener('mouseover', () => handleHover(event, 0.5));
<div id="nav">Mouse Over</div>

使用 bind不推荐,仅作说明):

const handleHover = () => { 
  event.target.style.opacity = opacity; 
}

nav.addEventListener('mouseover', handleHover.bind(event, opacity=0.5));
<div id="nav">Mouse Over</div>