如何使用 jquery 在通常使用 css 完成的链接上实现悬停效果:hover 来更改链接的颜色

发布于 2024-12-02 04:14:33 字数 186 浏览 1 评论 0原文

如何使用 jquery 在通常使用 css :hover 关闭的链接上实现悬停效果,因此当我将鼠标悬停在链接上时,它会更改为不同的颜色,当我的鼠标离开链接时,它会返回到之前的颜色?

编辑 我的链接有一个设置其颜色的内联样式,所以我尝试添加和删除类,但它们不起作用,似乎新添加的 css 类无法覆盖内联样式。

How do I use jquery to achieve hover effect on a link that is normally down with css :hover, so when I hover over a link it changes to a different color, and when my mouse leaves the link it returns to the color it had before?

Edit
My link has an inline style that sets its color, so I tried add and remove class they don't work, it seems the newly added css class cannot override the inline style.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作业与我同在 2024-12-09 04:14:33

一个简单的方法就是使用 jQuery.hover()

$('a#mylink').hover(function(){$(this).toggleClass("underline");},function(){$(this).toggleClass("underline");});

或者

$('a#mylink').hover(function(){$(this).css("text-decoration","underline");},function(){$(this).css("text-decoration","none");});

A simple way just use jQuery.hover():

$('a#mylink').hover(function(){$(this).toggleClass("underline");},function(){$(this).toggleClass("underline");});

or

$('a#mylink').hover(function(){$(this).css("text-decoration","underline");},function(){$(this).css("text-decoration","none");});
怕倦 2024-12-09 04:14:33

使用 mouseenter 和 mouseleave 事件。

$("#linkname").mouseenter(function(){
$(this).addClass("highlight");
});

$("#linkname").mouseleave(function(){
$(this).removeClass("highlight");
});

mouseover、mouseout 也可以工作。

对于链接,您可以使用带有 # 的 id 或带有 .

所以

这样你就可以使用;

$("#anchorid").mouseleave(

或者

$(".anchorclass").mouseleave(

use the mouseenter and mouseleave events.

$("#linkname").mouseenter(function(){
$(this).addClass("highlight");
});

$("#linkname").mouseleave(function(){
$(this).removeClass("highlight");
});

mouseover, mouseout will also work.

for the link you can use the id with the # or class name with the .

so

so you can then use;

$("#anchorid").mouseleave(

or

$(".anchorclass").mouseleave(
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文