突出显示当前侧边栏项目- javascript

snvhrwxg  于 2023-02-28  发布在  Java
关注(0)|答案(2)|浏览(97)

我试图让当前的侧边栏项目突出显示,我使用了以下代码:

<ul class="app-menu">
        <li><a class="app-menu__item" href="@Url.Action("Index","Home")">Accueil</a></li>
        <li><a class="app-menu__item" href="#">Categories</a></li>
        <li><a class="app-menu__item" href="@Url.Action("Index","Stock")">Stock</a></li>
        <li><a class="app-menu__item" href="@Url.Action("Index","Article")">Article</a></li>
</ul>

Javascript语言

<script>
    $(".app-menu li a").click(function () {
        $(".app-menu li a.active").removeClass("active");
        $(this).addClass("active");
    });
</script>

但是这个密码不起作用,这是怎么回事?

11dmarpk

11dmarpk1#

删除**"..."并使用...”**:

<li><a class="app-menu__item" href="@Url.Action('Index','Home')">Accueil</a></li>
jfgube3f

jfgube3f2#

请检查下面的代码,我认为您的href链接有问题

<html>

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    
  <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
  
  <style>
    .app-menu a.app-menu__item.active {
    font-weight:bold;
    color:#333;
    }
  </style>

</head>

<body>
<ul class="app-menu">
        <li><a class="app-menu__item" href="@Url.Action('Index','Home')">Accueil</a></li>
        <li><a class="app-menu__item" href="#">Categories</a></li>
        <li><a class="app-menu__item" href="@Url.Action('Index','Stock')">Stock</a></li>
        <li><a class="app-menu__item" href="@Url.Action('Index','Article')">Article</a></li>
</ul>

<script>
    $(".app-menu li a").click(function () {
        $(".app-menu li a.active").removeClass("active");
        $(this).addClass("active");
    });
</script>


</body>

</html>

相关问题