如何在纯JavaScript中模拟鼠标悬停,以激活CSS“:hover”? [英] How do I simulate a mouseover in pure JavaScript that activates the CSS ":hover"?

查看:1207
本文介绍了如何在纯JavaScript中模拟鼠标悬停,以激活CSS“:hover”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在Chrome中找到代码来模拟 mouseover ,但即使mouseover侦听器被触发,CSShover声明从未设置!

I've been trying to find code to simulate mouseover in Chrome but even though the "mouseover" listener gets fired, the CSS "hover" declaration is never set!

我也尝试过:

//Called within mouseover listener
theElement.classList.add("hover");

但是没有什么可以将元素更改为 hover 声明。

But nothing seems to change the element to what is declared in its hover declaration.

这是否可能?

推荐答案

p>你不能。这不是信任的活动

You can't. It's not a trusted event.


由用户代理生成的事件(作为用户交互的结果或作为对DOM的更改的直接结果)由用户代理信任,具有以下特权:不支持通过DocumentEvent.createEvent(Event)方法由脚本生成的事件,使用Event.initEvent()方法修改或通过EventTarget.dispatchEvent()方法分派的事件。受信任事件的isTrusted属性的值为true,而不受信任的事件的isTrusted属性值为false。

Events that are generated by the user agent, either as a result of user interaction, or as a direct result of changes to the DOM, are trusted by the user agent with privileges that are not afforded to events generated by script through the DocumentEvent.createEvent("Event") method, modified using the Event.initEvent() method, or dispatched via the EventTarget.dispatchEvent() method. The isTrusted attribute of trusted events has a value of true, while untrusted events have a isTrusted attribute value of false.

大多数不受信任的事件不应触发默认操作,但click或DOMActivate事件除外。

Most untrusted events should not trigger default actions, with the exception of click or DOMActivate events.

您必须手动添加一个类并在mouseover / mouseout事件上添加/删除。

You have to add a class and add/remove that on the mouseover/mouseout events manually.

这篇关于如何在纯JavaScript中模拟鼠标悬停,以激活CSS“:hover”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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