Please, considere this CSS code:

a { color: #ffcc00; }
a:hover { color: #ccff00; }

This HTML code:

<a href="#" id="link">Link</a>
<button id="btn">Click here</button>

And, finally, this JS code:

$("#btn").click(function() {
   $("#link").trigger("hover");
});

I would like to make my link uses its pseudo-class :hover when the button is clicked. I tried to trigger events like mousemove, mouseenter, hover etc, but anyone works. Notice that I want to force the use of the my CSS pseudo-class :hover specification and not use something like:

$("#link").css("color", "ccff00");

Some one know how do I do this? Thank you a lot.

推荐答案

You will have to use a class, but don't worry, it's pretty simple. First we'll assign your :hover rules to not only apply to physically-hovered links, but also to links that have the classname hovered.

a:hover, a.hovered { color: #ccff00; }

Next, when you click #btn, we'll toggle the .hovered class on the #link.

$("#btn").click(function() {
   $("#link").toggleClass("hovered");
});

If the link has the class already, it will be removed. If it doesn't have the class, it will be added.

Jquery相关问答推荐

如何使用 JQuery 将详细信息中的项目包装在容器中?

使用淡入淡出和追加

将一个类动态添加到 Bootstrap 的popover容器中

如何在 jQuery 中获取浏览器滚动位置?

如何在数据表中显示空数据消息

jQuery $.browser 是否已弃用?

ASP.Net 母版页和文件路径问题

如何通过 Select 插件使用 jQuery 验证?

jQuery UI 滑块(以编程方式设置)

Zepto 和 jQuery 2 有什么区别?

如何使用 jQuery Select sibling 元素?

如何从表格单元格 (td) 中获取相应的表格标题 (th)?

addClass - 可以在同一个 div 上添加多个类吗?

jquery变量语法

如何在 Angular 4 中使用 jQuery 插件?

你如何在Javascript中缓存图像

触发下拉更改事件

订单对象是否由指定的 jQuery Select 器返回?

通过单击按钮获取表格行的内容

在事件中使用 jQuery 获取被点击的元素?