HTML/CSS 如何关闭菜单,然后使用 class=“active”

HTML/CSS How to make a menu closed and then use class="active"

本文关键字:class active 然后 CSS 何关闭 菜单 HTML      更新时间:2023-09-26

我想关闭一个菜单,当我访问一个 URL 时,<li>应该将他的类更改为 class="active",但在开始时我想让该类保持没有 class="active"。

我想知道哪种方式是最好的做这个Javascript,或者它可以只用css来完成。

使用 jQuery 的最佳方式

$(document).ready(function(){
var str=location.href.toLowerCase();
$(".navigation li a").each(function() {
if (str.indexOf(this.href.toLowerCase()) > -1) {
 $("li.active").removeClass("active");
$(this).parent().addClass("active");
}
 });
 })