javascript 保持活动菜单项突出显示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19166890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 14:38:23  来源:igfitidea点击:

Keep active menu item highlighted

javascriptjquerycss

提问by user1938745

How would I make it so that the item that is clicked in the menu, stays highlighted blue. So basically the active menu item.

我将如何使在菜单中单击的项目保持突出显示为蓝色。所以基本上是活动菜单项。

Fiddle

小提琴

I've tried using css active, but im thinking I need javascript or something.

我试过使用 css active,但我想我需要 javascript 或其他东西。

 #cssmenu > ul li > a:active, #cssmenu > ul li:active > a {
    color: #ffffff;
    text-shadow: 0 1px 0 rgba(0, 0, 0, 0.3);
    background: #54cdf1;
    background: -webkit-linear-gradient(#72d4f2, #54cdf1);
    background: -moz-linear-gradient(#72d4f2, #54cdf1);
    background: linear-gradient(#72d4f2, #54cdf1);
    border-color: transparent;
}

回答by Orlando

You'll need some JS for that.

为此,您需要一些 JS。

$(document).ready(function() {
    $("#cssmenu li").on("click", function() {
        $("#cssmenu li").removeClass("active");
        $(this).addClass("active");
    });
});

Then just style #cssmenu li.activethe way you want it in your CSS.

然后#cssmenu li.active在 CSS 中按照您想要的方式设置样式。