jquery:仅在鼠标悬停时将类添加到列表项 - 鼠标移出时删除?
嘿伙计们, 最好的方法是什么:
我有一个列表
<ul>
<li>hello</li>
<li>good bye</li>
<li>arrivederci</li>
<li class="selected">dude</li>
<li>whatever</li>
</ul>
最初一个项目已经应用了 .selected 类。当我将鼠标悬停在其中一个列表项上时,我希望该列表项具有 .selected 类。因此,每个项目都应该只在我结束时应用该类,一旦我离开该项目,该类就会被删除,下一个就有该类。
hey guys,
what's the best way to do that:
i have a list
<ul>
<li>hello</li>
<li>good bye</li>
<li>arrivederci</li>
<li class="selected">dude</li>
<li>whatever</li>
</ul>
Initially one item already has a class of .selected applied. When i hover over one of the list-items i want this one to have the .selected class. So every item should only have the class applied when im over, as soon as i leave the item the class get's removed and the next one has the class.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据您的描述,我认为您想要这个:
演示: http://jsfiddle.net/swN5F/
或者,更有可能你想要的是:
演示: http://jsfiddle.net/swN5F/1/ (请注意,最后悬停的项目保持红色)
更新:适用于多个
的版本,并使用向上/向下箭头键:
演示:http://jsfiddle.net/zBjrS/36/
From your description, I think you want this:
Demo: http://jsfiddle.net/swN5F/
Or, more likely what you want is actually:
Demo: http://jsfiddle.net/swN5F/1/ (note that the last-hovered item remains red)
Update: version that works with multiple
<ul>
s, and with up/down arrow keys:Demo: http://jsfiddle.net/zBjrS/36/
为什么不使用内置浏览器功能:
标记:
CSS:
这与您要求的有点不同,因为一开始不会选择任何内容,但是如果通过悬停进行选择,为什么应该选择某些内容..
< a href="http://jsbin.com/inigi3" rel="nofollow">预览
Why don't you use built-in browser feature:
Markup:
CSS:
This is a little bit different than what you asked because nothing will be selected at first, but why something should be selected if the selection is made by hover..
Preview
您可以使用 jQuery 的
hover
来做到这一点:实时示例
这会将“选定”类切换到鼠标经过的
li
,如果鼠标离开一个li
并且不进入另一个,它将删除该类(没有一个类将具有“选定的”类)。我认为这就是您所说的您想要的,尽管它与其中一个一开始就已经拥有“选定”课程的情况不符。如果您只想在某人将鼠标悬停在
li
上时更改它,但不想在鼠标离开li
而不输入另一个时删除它,请使用 < code>mouseenter:实例
如果您查找
mouseenter
各种参考文献中的事件,他们会告诉你这是 IE 特定的。确实如此,但它非常有用,以至于 jQuery 在其他浏览器上模拟了它。更新:如果您愿意,可以将上面的
siblings("li")
更改为siblings("li.selected")
,例如:这可能会稍微提高效率(不要试图从已经拥有的类中删除一个类)。 这是第一个示例(带有
hover
),已更新以实现此目的; 这是第二个示例(使用mouseenter
)进行更新以实现此目的。离题:但是根据您想要的效果,您也许可以使用 CSS 而不是使用 jQuery 和“选定”类来实现它。如果您只想在鼠标实际悬停在
li
上时突出显示它(上面的hover
示例),而不是让最后一个li
突出显示(上面的mouseenter
示例),如果不需要支持 IE6,可以使用 CSS:hover
伪类:Live example 但同样,只有当您想要悬停效果时,并且仅当您不需要支持 IE6(仅允许
:hover
在a
元素上)。You can do that, using jQuery's
hover
:Live example
That will switch the "selected" class to the
li
that the mouse is over, and if the mouse leaves oneli
and doesn't enter another, it will remove the class (no none of them will have the "selected" class). I think that's what you said you wanted, although it doesn't match up with having one of them already have the "selected" class at the outset.If you just want to change it when someone mouses over an
li
but not remove it when the mouse leaves anli
without entering another, usemouseenter
:Live example
If you look up the
mouseenter
event in various references, they'll tell you it's IE-specific. That's true, but it's so useful that jQuery emulates it on other browsers.Update: You can change the
siblings("li")
tosiblings("li.selected")
in the above if you like, e.g.:That may make it slightly more efficient (not trying to remove a class from something that already has it). Here's the first example (with
hover
) updated to do that; here's the second example (withmouseenter
) updated to do that.Off-topic: But depending on which effect you want, you may be able to achieve it with CSS rather than with jQuery and a "selected" class. If you just want the
li
highlighted when the mouse is actually over it (thehover
example above), rather than leaving the lastli
highlighted (themouseenter
example above), and if you don't need to support IE6, you can do it using the CSS:hover
pseudoclass:Live example But again, that's only if you want the hover effect, and only if you don't need to support IE6 (which only allows
:hover
ona
elements).