使用 JQuery 悬停项目
有没有办法使用 javascript 悬停元素? 我不想创建另一个类,我只是想当我的鼠标指针不在该元素上时使该元素通过 javascript 悬停。
例如,我有 5 个具有相同类的元素,当其中一个元素实际悬停时,我想对所有元素调用悬停。
Is there a way to hover an element using javascript?
I don't want to create another class, I just want to cause element to hover with javascript when my mouse pointer is not over that element.
For example I have 5 elements with the same class and I want to call hover on all of them when one of them is actually hovered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设您指的是与链接关联的伪类
:hover
(例如)。当您将鼠标悬停在该链接上时,您希望调用所有其他链接的:hover
样式。不幸的是,您无法从 jQuery 调用
:hover
样式,这需要您实际将鼠标指针移到该元素上。您必须使用类并利用 jQuery 的悬停事件。I assume you mean the pseudo class
:hover
that you've associated with a link (for example). As you hover over that link, you want to invoke all other link's:hover
styles.Unfortunately, you can not invoke the
:hover
styles from jQuery, that requires that you actually move your mouse pointer over that element. You have to use classes and utilize jQuery's hover event.您可以通过在悬停事件处理程序中同时处理集合中的所有项目来实现此目的
You can achieve this by addressing all of the items in your collection at the same time in your hover event handlers
如果我正确理解您的问题,您已经使用 jQuery 添加了一个
hover
事件,并且您希望手动触发该事件,而不管鼠标如何。如果我理解正确的话,你想调用
mouseenter
触发mouseenter
事件。如果我理解错误,并且您实际上有一个
:hover
CSS 规则并希望使用 Javascript 触发,那么这是不可能的。相反,您应该向规则添加一个类名(例如,
something:hover, some.FakeHover { ... }
),并使用 jQuery 添加该类名。 (例如,$(...).addClass('FakeHover')
)。If I understand your question correctly, you've added a
hover
event using jQuery, and you'd like to trigger that event manually regardless of the mouse.If I understood correctly, you want to call the
mouseenter
to trigger themouseenter
event.If I've understood incorrectly, and you actually have a
:hover
CSS rule which you'd like to trigger using Javascript, that's not possible.Instead, you should add a class name to the rule (eg,
something:hover, something.FakeHover { ... }
), and add that class name using jQuery. (eg,$(...).addClass('FakeHover')
).在 jQuery 中,
trigger
函数允许您触发事件(我相信包括鼠标悬停
)在元素上。在直接的 JavaScript 中,如果您已将一个函数分配给元素的事件处理程序,那么您当然可以随时调用它。例如
In jQuery, the
trigger
function allows you to trigger events (includingmouseover
, I believe) on elements.In straight JavaScript, if you’ve assigned a function to an element’s event handler, you can of course call that whenever you want. E.g.