jquery:仅在鼠标悬停时将类添加到列表项 - 鼠标移出时删除?

发布于 2024-10-11 23:07:04 字数 388 浏览 7 评论 0原文

嘿伙计们, 最好的方法是什么:

我有一个列表

<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 技术交流群。

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

发布评论

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

评论(3

带刺的爱情 2024-10-18 23:07:04

根据您的描述,我认为您想要这个:

$('ul > li').hover(function () {
    $(this).toggleClass('selected').siblings().removeClass('selected');
});

演示: http://jsfiddle.net/swN5F/

或者,更有可能你想要的是:

$('ul > li').mouseenter(function () {
    $(this).addClass('selected').siblings().removeClass('selected');
});

演示: http://jsfiddle.net/swN5F/1/ (请注意,最后悬停的项目保持红色)


更新:适用于多个

    的版本,并使用向上/向下箭头键:

var $activeUl = $('ul:first'); // For keyboard arrows

$('ul > li').mouseenter(function () {
    $(this).addClass('selected').siblings().removeClass('selected');
    $activeUl = $(this).parent();
});

$(document).keydown(function (e) {
    var direction, siblingsSelector;
    if (e.which == 38) { // up
        direction = 'prev';
        siblingsSelector = ':not(:first-child)';
    } else if (e.which == 40) { // down
        direction = 'next';
        siblingsSelector = ':not(:last-child)';
    }
    $activeUl.find('.selected')[direction]().addClass('selected')
        .siblings(siblingsSelector).removeClass('selected');
});

演示:http://jsfiddle.net/zBjrS/36/

From your description, I think you want this:

$('ul > li').hover(function () {
    $(this).toggleClass('selected').siblings().removeClass('selected');
});

Demo: http://jsfiddle.net/swN5F/

Or, more likely what you want is actually:

$('ul > li').mouseenter(function () {
    $(this).addClass('selected').siblings().removeClass('selected');
});

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:

var $activeUl = $('ul:first'); // For keyboard arrows

$('ul > li').mouseenter(function () {
    $(this).addClass('selected').siblings().removeClass('selected');
    $activeUl = $(this).parent();
});

$(document).keydown(function (e) {
    var direction, siblingsSelector;
    if (e.which == 38) { // up
        direction = 'prev';
        siblingsSelector = ':not(:first-child)';
    } else if (e.which == 40) { // down
        direction = 'next';
        siblingsSelector = ':not(:last-child)';
    }
    $activeUl.find('.selected')[direction]().addClass('selected')
        .siblings(siblingsSelector).removeClass('selected');
});

Demo: http://jsfiddle.net/zBjrS/36/

暖伴 2024-10-18 23:07:04

为什么不使用内置浏览器功能:

标记:

<ul class="hoverUl">
    <li>hello</li>
    <li>good bye</li>
    <li>arrivederci</li>
    <li>dude</li>
    <li>whatever</li>
</ul>

CSS:

.hoverUl li { /* normal style */ }
.hoverUl li:hover { /* "selected" style */ }

这与您要求的有点不同,因为一开始不会选择任何内容,但是如果通过悬停进行选择,为什么应该选择某些内容..

< a href="http://jsbin.com/inigi3" rel="nofollow">预览

Why don't you use built-in browser feature:

Markup:

<ul class="hoverUl">
    <li>hello</li>
    <li>good bye</li>
    <li>arrivederci</li>
    <li>dude</li>
    <li>whatever</li>
</ul>

CSS:

.hoverUl li { /* normal style */ }
.hoverUl li:hover { /* "selected" style */ }

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

素染倾城色 2024-10-18 23:07:04

可以使用 jQuery 的 hover 来做到这一点:

$("selector_for_your_ul li").hover(
    function() {
        $(this).addClass("selected").siblings("li").removeClass("selected");
    },
    function() {
        $(this).removeClass("selected");
    }
);

实时示例

这会将“选定”类切换到鼠标经过的 li,如果鼠标离开一个 li 并且进入另一个,它将删除该类(没有一个类将具有“选定的”类)。我认为这就是您所说的您想要的,尽管它与其中一个一开始就已经拥有“选定”课程的情况不符。

如果您只想在某人将鼠标悬停在 li 上时更改它,但不想在鼠标离开 li 而不输入另一个时删除它,请使用 < code>mouseenter:

$("selector_for_your_ul li").mouseenter(
    function() {
        $(this).addClass("selected").siblings("li").removeClass("selected");
    }
);

实例

如果您查找 mouseenter各种参考文献中的事件,他们会告诉你这是 IE 特定的。确实如此,但它非常有用,以至于 jQuery 在其他浏览器上模拟了它。

更新:如果您愿意,可以将上面的 siblings("li") 更改为 siblings("li.selected"),例如:

$(this).addClass("selected").siblings("li.selected").removeClass("selected");

这可能会稍微提高效率(不要试图从已经拥有的类中删除一个类)。 这是第一个示例(带有 hover),已更新以实现此目的; 这是第二个示例(使用 mouseenter)进行更新以实现此目的。


离题:但是根据您想要的效果,您也许可以使用 CSS 而不是使用 jQuery 和“选定”类来实现它。如果您只想在鼠标实际悬停在 li 上时突出显示它(上面的 hover 示例),而不是让最后一个 li 突出显示(上面的 mouseenter 示例),如果不需要支持 IE6,可以使用 CSS :hover 伪类:

selector_for_your_ul li:hover {
    color: red; /* ...or whatever... */
}

Live example 但同样,只有当您想要悬停效果时,并且仅当您不需要支持 IE6(仅允许 :hovera 元素上)。

You can do that, using jQuery's hover:

$("selector_for_your_ul li").hover(
    function() {
        $(this).addClass("selected").siblings("li").removeClass("selected");
    },
    function() {
        $(this).removeClass("selected");
    }
);

Live example

That will switch the "selected" class to the li that the mouse is over, and if the mouse leaves one li 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 an li without entering another, use mouseenter:

$("selector_for_your_ul li").mouseenter(
    function() {
        $(this).addClass("selected").siblings("li").removeClass("selected");
    }
);

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") to siblings("li.selected") in the above if you like, e.g.:

$(this).addClass("selected").siblings("li.selected").removeClass("selected");

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 (with mouseenter) 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 (the hover example above), rather than leaving the last li highlighted (the mouseenter example above), and if you don't need to support IE6, you can do it using the CSS :hover pseudoclass:

selector_for_your_ul li:hover {
    color: red; /* ...or whatever... */
}

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 on a elements).

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