删除/解除绑定悬停在锚点上
html
<a href="home.html">Home</a>
css
a {
color: blue;
}
a:hover {
color: red;
}
现在,您可以看到 现在悬停时将显示为红色。
问题
如何通过 jQuery 删除悬停?
我尝试过: $('a').unbind('hover');
和 $('a').unbind('mouseenter mouseleave')
我开始思考为什么它会赢不行,这不是hover()
吗?
html
<a href="home.html">Home</a>
css
a {
color: blue;
}
a:hover {
color: red;
}
now as you can see <a>
now would be color red on hover.
Question
How do I remove hover via jQuery?
I have tried:$('a').unbind('hover');
and $('a').unbind('mouseenter mouseleave')
I come to think why it won't work, is this not hover()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
由于
a:hover
不是锚标记上的绑定事件,而只是一个伪类,因此您无法成功解除 .hover() 事件的绑定。如果你想改变行为,那么你可以做两件事
删除
a:hover
样式绑定在锚标记上绑定一个悬停事件并相应地设置 css。
Since
a:hover
is not bound event on the anchor tag and is only a pseudo class you won't have success unbinding the .hover() event.If you want to change the behavior then you can do two things
remove the
a:hover
stylesbind a hover event on the anchor tag and set the css accordingly.
不。这是 CSS 规则,而不是 JavaScript 事件。
更改颜色的最简单方法是通过更强大的 CSS 规则,例如:
或
No. This is a CSS rule, not a JavaScript event.
The easiest way to change the color is via a stronger CSS rule, for example:
or
您可以添加/删除类并使用 JQuery 进行相应操作。因此,您应该为正常状态和悬停状态创建类。例如,您可以像这样从元素中删除样式:
但我建议您使用以下方法实际添加和删除类:
addClass()
removeClass()
You can add/remove classes and use JQuery to act accordingly. So you should create classes for both normal and hover state. For example, you can remove styling from the element like this:
But I would suggest you to actually add and remove classes accordingly using:
addClass()
removeClass()
如果您担心它是唯一的颜色,这可能会有所帮助。
元素的
style
属性将覆盖任何 CSS 选择器中设置的属性。该理论已经过测试并且运行良好。该插件将读取任意 css 属性并将它们设置回来,从而“粘住”它们。If its only color you are concerned about this might help.
The element's
style
property will override the properties set in any CSS selectors. The theory has been tested and works fine. This plugin will read arbitrary css properties and set them back therefore "sticking" them.