jQuery 悬停和单击功能
我有 4 个链接,可以更改网页内 4 个 div 的位置。当我将鼠标悬停在链接上时,我使用以下 jQuery 脚本来更改链接的颜色。
$('a.menua').hover(function(){
$(this).css({'color':'#2EC7C7'});
},
function(){
$(this).css({'color':'white'});
});
我如何修改此脚本,以便当我单击链接时,它可以保持颜色不悬停,并且当我将鼠标从其上移开时不会改变?
$('a.menua').click(function(){
//content
});
I have 4 links that change the position of 4 divs inside a web page. I am using the following jQuery script to change the color of the links when I hover them.
$('a.menua').hover(function(){
$(this).css({'color':'#2EC7C7'});
},
function(){
$(this).css({'color':'white'});
});
how can i modify this script so that when i click a link it keeps the color from hover and not change when i take my mouse off of it?
$('a.menua').click(function(){
//content
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我将使用 CSS 来实现所有样式,如下所示:
然后仅使用 jQuery,只需使用
切换该类。像这样的toggleClass()
:或者,如果您一次只需要一个活动:
这会使您的代码更简单、更轻且更易于维护。它还将样式信息保留在它所属的位置(如果可能的话),即 CSS 中。
I would use CSS for all of the styling, like this:
Then only use jQuery just toggle that class using
.toggleClass()
like this:Or, if you want only one active at a time:
This makes your code simpler, lighter, and easier to maintain. Also it keeps your styling information where (if at all possible) it belongs, in the CSS.
唯一需要 JS 的部分是让链接在鼠标离开后保持相同的颜色。仅 CSS 就可以让您控制鼠标悬停时(使用
a:hover
)和鼠标单击期间(使用a:active
)时的颜色。Craver 建议使用 jQuery 添加一个类,应该在您离开后保留颜色,正如他所说,将样式信息保留在 CSS 中是很好的。
如果您使用所有四种可能的链接样式,请确保按以下顺序放置它们:
您可以用 LoVe HAte 来记住它 - 链接、已访问、悬停、活动。
另一种想法 - 您试图在悬停和单击期间使链接颜色相同。我建议让它们有点不同可能会更好。点击过程中颜色的变化会给用户视觉反馈,表明他们击中了目标。
The only part that should require JS is for the link to keep the same color after you take your mouse off of it. CSS alone will let you control what color it has when you're hovering (with
a:hover
) and during the mouse click (witha:active
).Craver's suggestion to add a class with jQuery should take care of keeping the color after you move away, and as he said, it's nice to keep the style info in your CSS.
If you use all four possible link styles, make sure you put them in this order:
You can remember it with LoVe HAte - Link, Visited, Hover, Active.
One other thought - you're trying to make the link color identical during hover and click. I'd suggest it may be better to let them be a little different. Having the color change during the click gives the user visual feedback that they hit the target.
使用 element.Data:
但是 Nicks css 版本
可能是更好的选择。use element.Data:
But Nicks css version is
probablythe better way to go.我想你想要这个:
实时解决方案
因此,我使用 jQuery 向链接添加类。我还将脚本设置为一次只允许一项处于活动状态(这是此解决方案与 Nick 的解决方案之间的主要区别)。
更新:
悬停效果现在也是基于 CSS 的(这就是 :hover 伪类的用途)。因此,您只需使用 jQuery 来设置链接的“活动”状态。
HTML:
CSS:
JS:
I think you want this:
live solution
So, I use jQuery to add classes to links. I also set the script to let only one item be active at a time (that's the main difference between this solution and Nick's).
update:
The hovering-effect is now also CSS based (That is what the :hover pseudo class is for). So you only use jQuery to set the "active" state of the link.
HTML:
CSS:
JS:
点击后添加一个班级。
js
Add a class after click.
css
js
我还没有测试过,但这可能有效:
I haven't tested it, but this might work:
js:
CSS:
js:
css: