是否可以使用 AddEventListener 为悬停/输入事件添加侦听器?

发布于 2024-12-18 18:36:42 字数 304 浏览 2 评论 0原文

我正在尝试添加一个事件侦听器来响应悬停在 div 上并更改其颜色。

我必须使用 addEventListener ...这是一项作业。我不确定这怎么可能?

就像我可以这样使用它:

myButtonItem.addEventListener("click",myrespondfunction,false) 

将单击事件处理程序分配给 myButtonItem...但是没有可以代替 click"hover"

I am trying to add an event listener to respond to hovering over a div and changing its color.

I have to use addEventListener ...this is an assignment. I am not sure how that would be possible though?

Like I can use it like this:

myButtonItem.addEventListener("click",myrespondfunction,false) 

to assign a click event handler to myButtonItem...but there is no "hover" that I can put in place of click.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌︼了一个春 2024-12-25 18:36:42

有几种方法可以实现悬停效果。最好的方法是使用 CSS 选择器:

.myDiv {
    background-color: red;
}
.myDiv:hover {
    background-color: green;
}

...当鼠标进入 div 时,具有类 myDiv 的 div 将显示绿色背景。

第二种方法是使用 javascript,尽管考虑到 :hover 伪选择器可用并得到广泛支持,这对于简单地更改背景颜色来说是一种极端的矫枉过正。

您必须选择的事件有:

mouseenter - 当鼠标进入元素时触发。不冒泡,目前 Firefox、Safari 或 Chrome 不支持,虽然它在规范中并且已经存在了一段时间

mouseleave - 当鼠标离开元素时触发。不冒泡,目前 Firefox、Safari 或 Chrome 不支持,虽然它在规范中并且已经存在了一段时间

mouseover - 鼠标进入元素时触发,冒泡到父元素

mouseout - 鼠标离开元素时触发,冒泡到父元素

查看 http://www.quirksmode.org/js/events_mouse.html 了解与这些事件相关的特定怪癖的更多信息。由于与多个事件处理程序相关的权重(这就是为什么不建议使用 javascript 来处理 CSS 可以处理的内容...您向页面添加了不必要的权重),您可能需要考虑使用事件委托来处理这些事件。请参阅 http://www.sitepoint.com/javascript -event-delegation-is-easier-than-you-think/ 是一篇入门级文章,解释了这个概念并研究了一些优点和缺点。例如,如果计划将其添加到页面上的所有 a 标记中,我将毫无疑问地使用委托。

就实际添加事件而言,addEventListener 不是跨浏览器的。请参阅文章http://www.quirksmode.org/js/events_advanced.html有关该问题的信息,以及讨论不同之处并提供的 MDN 文档跨浏览器解决方法。

There are a few ways to accomplish hover effects. The best way is to use CSS selectors:

.myDiv {
    background-color: red;
}
.myDiv:hover {
    background-color: green;
}

... a div with the class myDiv would show a green background when the mouse enters the div.

A second approach is to use javascript, though this is an extreme overkill for simply changing the background color, considering that the :hover pseudo-selector is available and widely supported.

The events you have to choose from are:

mouseenter - Fired when the mouse enters an element. Does not bubble, not currently supported by Firefox, Safari, or Chrome, though it is in the spec and has been for some time.

mouseleave - Fired when the mouse leaves an element. Does not bubble, not currently supported by Firefox, Safari, or Chrome, though it is in the spec and has been for some time.

mouseover - Fired when the mouse enters an element, bubbles to parent elements

mouseout - Fired when the mouse leaves element, bubbles to parent elements

Check out http://www.quirksmode.org/js/events_mouse.html for more information about specific quirks related to these events. Because of the weight associated with multiple event handlers (that's why it isn't recommended to use javascript to handle something that CSS can handle... you're adding unnecessary weight to the page), you may want to consider using event delegation to handle these events. See http://www.sitepoint.com/javascript-event-delegation-is-easier-than-you-think/ for an entry-level article explaining the concept and examining some of the pros and cons. If the plan is to add this to all a tags on the page, for example, I would use delegation without question.

As far as actually adding the events, addEventListener is not cross-browser. See the article http://www.quirksmode.org/js/events_advanced.html for information on the issue, and the MDN documentation which discusses the different and offers cross-browser workaround approaches.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文