使用 JavaScript 事件模拟悬停

2022-01-22 00:00:00 events hover javascript

是否可以使用 JavaScript 事件模拟悬停?我尝试在目标元素上注入鼠标悬停事件,但没有运气.

Is it possible to simulate hover using JavaScript Events? I tried injecting mouseover event on the target element but no luck.

例如,如果有一个带有悬停选择器的链接,是否可以使用 JavaScript 事件悬停"它?基本上,我想触发 CSS 悬停.你可以假设我不能使用 jQuery.

For example, if there is a link that has a hover selector, is it possible to "hover" over it using JavaScript Events? Basically, I want to trigger CSS hover. You can assume I can't use jQuery.

推荐答案

jQuery hover 事件只是使用了 mouseentermouseleave 事件.这是jQuery的悬停来源:

The jQuery hover event is just using mouseenter and mouseleave events. Here is the source of jQuery's hover:

function (fnOver, fnOut) {
    return this.mouseenter(fnOver).mouseleave(fnOut || fnOver);
}

相关文章