31

我想突出显示您单击的当前菜单。我正在使用 CSS,但它现在正在工作。

这是我的CSS代码:

#sub-header ul li:hover{ background-color: #000;}
#sub-header ul li:hover a{ color: #fff; }
#sub-header ul li.active{ background-color: #000; }
#sub-header ul li.active a{ color: #fff; }

这是我的html:

<div id="sub-header">
    <ul>
        <li> <a href="index.php">Home</a> </li>
        <li> <a href="contact.php">Contact Us</a> </li>
        <li> <a href="about.php">About Us</a> </li>
    </ul>
</div>

当我悬停并且菜单处于活动状态时,这就是我喜欢的

当我悬停并且菜单处于活动状态时,这就是我喜欢的

悬停是可以的,问题是当我点击菜单后黑地不显示


@Jonathan,我已经解决了它,它比你给出的更简单。

这是我的回答:

$(function(){
    // this will get the full URL at the address bar
    var url = window.location.href; 

    // passes on every "a" tag 
    $("#sub-header a").each(function() {
            // checks if its the same on the address bar
        if(url == (this.href)) { 
            $(this).closest("li").addClass("active");
        }
    });
});

然后在我的css文件上:

.active { background-color: #000; }
/* to override the existing css for "a" tag */
#sub-header .active a{ color: #fff; }
4

11 回答 11

36

在每个页面的正文中添加一个类:

<body class="home">

或者,如果您在联系页面上:

<body class="contact">

然后在创建样式时考虑到这一点:

#sub-header ul li:hover,
body.home li.home,
body.contact li.contact { background-color: #000;}

#sub-header ul li:hover a,
body.home li.home a,
body.contact li.contact a { color: #fff; }

最后,将类名应用于您的列表项:

<ul>
  <li class="home"><a href="index.php">Home</a></li>
  <li class="contact"><a href="contact.php">Contact Us</a></li>
  <li class="about"><a href="about.php">About Us</a></li>
</ul>

此时,无论何时您在body.home页面上,您的li.home a链接都将具有默认样式,表明它是当前页面。

于 2012-05-18T04:44:31.130 回答
2

答:你需要 CSS 来表示“当前”链接,这里是 tut。

jQuery 菜单导航的描述

示例:众多解决方案之一

它为我工作

于 2012-11-23T00:51:13.407 回答
2

添加简单的方式

<div id='cssmenu'>
<ul>
<li class=''><a href='1.html'><span>1</span></a></li>
<li class=''><a href='2.html'><span>2</span></a></li>
<li class='' style="float:right;"><a href='3.html'><span>3</span></a></li>
</ul>
</div>

$("document").ready(function(){
$(function() {
$('.cssmenu a[href="' + location.pathname.split("/")[location.pathname.split("/").length-1] + '"]').parent().addClass('active');
});

});
于 2014-05-22T12:35:32.690 回答
2

由于这个问题的给定标签是CSS,我将提供我完成它的方式。

  1. 在 .css 文件中创建一个类。

    a.activePage{ color: green; border-bottom: solid; border-width: 3px;}

  2. 导航栏将类似于:

    • 联系我们
    • 关于我们

注意:如果您已经使用一个类为所有 Nav-Bar 元素设置样式,您可以在 html-tag 中的泛型类之后使用空格级联我们创建的特殊情况类。

示例:在这里,我已经从我为所有 list-items 创建的名为'navList'的类中导入了我的样式。但是特殊情况的样式属性是“activePage”类的一部分

.CSS 文件:

a.navList{text-decoration: none; color: gray;}
a.activePage{ color: green; border-bottom: solid; border-width: 3px;}

.HTML 文件:

<div id="sub-header">
    <ul>
        <li> <a href="index.php" class= "navList activePage" >Home</a> </li>
        <li> <a href="contact.php" class= "navList">Contact Us</a> </li>
        <li> <a href="about.php" class= "navList">About Us</a> </li>
    </ul>
</div>

看看我是如何将一个类名级联的。

于 2014-08-24T06:51:02.700 回答
1

也许这是非常非常糟糕的方式,只适合懒惰的人,但我决定说出来。

我也使用了 PHP 和 bootstrap 和 fontawsome

简单来说,我有 2 页:1.index 和 2.create-user

将此代码放在您的代码上方

<?php
$name=basename($_SERVER["REQUEST_URI"]);
$name=str_replace(".php","",$name);
switch ($name) {
    case "create-user":
        $a = 2;
        break;
    case "index":
        $a = 1;
        break;
    default:
        $a=1;
}
?>

并在菜单<?php if($a==1){echo "active";} ?>中为 menu1 添加类,为 menu2 添加<?php if($a==2){echo "active";} ?>

<ul id="menu" class="navbar-nav  flex-column text-right mt-3 p-1">
                        <li class="nav-item mb-2">
                            <a href="index.php" class="nav-link text-white customlihamid <?php if($a==1){echo "active";} ?>"><i
                                        class="fas fa-home fa-lg text-light ml-3"></i>dashbord</a>
                        </li>
                        <li class="nav-item mb-2">
                            <a href="#" href="javascript:" data-parent="#menu" data-toggle="collapse"
                               class="accordion-toggle nav-link text-white customlihamid <?php if($a==2){echo "active";} ?>" data-target="#tickets">
                                <i class="fas fa-user fa-lg text-light ml-3"></i>manage users
                                <span class="float-left"><i class="fas fa-angle-down"></i></span>
                            </a>

                            <ul class="collapse list-unstyled mt-2 mr-1 pr-2" id="tickets">
                                <li class="nav-item mb-2">
                                    <a href="create-user.php" class="nav-link text-white customlihamid"><i class="fas fa-user-plus fa-lg text-light ml-3"></i>add user</a>
                                </li>

                                <li class="nav-item mb-2">
                                    <a href="#" class="nav-link text-white customlihamid"><i class="fas fa-user-times fa-lg text-light ml-3"></i>delete user</a>
                                </li>

                            </ul>
                        </li>

                    </ul>

并添加CSS

.customlihamid {
    transition: all .4s;
}

.customlihamid:hover {
    background-color: #8a8a8a;
    border-radius: 5px;
    color: #00cc99;
}
.nav-item > .nav-link.active  {
    background-color: #00cc99;
    border-radius: 7px;
    box-shadow: 5px 7px 10px #111;
    transition: all .3s;
}
.nav-item > .nav-link.active:hover  {
    background-color: #8eccc1;
    border-radius: 7px;
    box-shadow: 5px 7px 20px #111;
    transform: translateY(-1px);
}

并在js中添加

$(document).ready(function () {
   $('.navbar-nav .nav-link').click(function(){
      $('.navbar-nav .nav-link').removeClass('active');
      $(this).addClass('active');
   })
});

首先检查你的工作没有 js 代码,以了解 js 代码是为了什么

于 2019-05-20T20:02:44.547 回答
0

首先,给所有链接一个唯一的 id 并创建一个名为 active 的 css 类:

<ul>
    <li><a id="link1" href="#/...">link 1</a></li>
    <li><a id="link2" href="#/...">link 2</a></li>
</ul>

CSS:

.active {
    font-weight: bold;
}

jQuery版本:

function setActiveLink(setActive){
    if ($("a").hasClass('active'))
        $("a").removeClass('active');
    if (setActive)
        $("#"+setActive).addClass('active');
}

$(function() {
    $("a").click(function() {
        setActiveLink(this.id);
    });
});

香草 javascript 版本:

为了防止使用 选择过多的链接document.querySelectorAll,请给父元素一个名为 menuLinks 的 id。在链接上添加一个 onClick 处理程序。

<ul id="menuLinks">
    <li><a id="link1" href="#/..." onClick="setActiveLink(this.id);">link 1</a></li>
    <li><a id="link2" href="#/..." onClick="setActiveLink(this.id);">link 2</a></li>
</ul>

代码:

function setActiveLink(setActive){
    var links = document.querySelectorAll("#menuLinks a");
    Array.prototype.map.call(links, function(e) {
        e.className = "";
        if (e.id == setActive)
            e.className = "active";
    })
}
于 2015-07-23T12:14:26.783 回答
0

您应该引用当前元素,而不是所有与您的选择器匹配的元素。

$("#mainMenu td").click(function() {
$(this).css('background-color', '#EDEDED');

});

我还建议您使用 CSS 类而不是这样设置 CSS 属性。

那会是这样的;

$("#mainMenu td").click(function() {
$(this).addClass('selected');

});

和...一起;

#mainMenu td.selected {

背景颜色:#EDEDED;}

于 2015-09-18T10:06:30.007 回答
0

试试这个(复制和粘贴):

测试.html:-

<html>
 <link rel="stylesheet" type="text/css" href="style.css">
 <a class="fruit" href="#">Home</a></span>
 <a class="fruit"  href="#">About</a></span>
 <a class="fruit"  href="#">Contact</a></span>
</html>

样式.css:-

a:link{
 color:blue;
}

a:visited{
 color:purple;
}

a:hover{
 color:orange;
}
a:focus{
color:green;
}

a:active{
 color:red;
}

a:active{
 color:yellow;
}
于 2015-10-05T10:31:23.230 回答
0

另一种带有简单 2 线监听器的变体

$( ".menu_button" ).click(function() {

    $( ".menu_button" ).removeClass('menu_button_highlight');
    $(this).addClass('menu_button_highlight');
});

=====

    <a class='menu_button' href='#admin'>Admin</a>
    <br/>

    <a class='menu_button' href='#user_manager'>User Manager</a>
    <br/>

    <a class='menu_button' href='#invite_codes'>Invite Codes</a>

====

.menu_button {

    padding: 0 5px;
}

.menu_button_highlight {

    background: #ffe94c;
}
于 2018-04-14T00:18:37.520 回答
0

假设我们有一个这样的菜单:

<div class="menu">
  <a href="link1.html">Link 1</a>
  <a href="link2.html">Link 2</a>
  <a href="link3.html">Link 3</a>
  <a href="link4.html">Link 4</a>
</div>

让我们当前的网址为https://demosite.com/link1.html

使用下面的函数,我们可以在我们的 url 中添加菜单的 href 所在的活动类。

let currentURL = window.location.href;

document.querySelectorAll(".menu a").forEach(p => {
  if(currentURL.indexOf(p.getAttribute("href")) !== -1){
    p.classList.add("active");
  }
})
于 2020-04-21T20:56:39.560 回答
0

按照@Sampson的回答,我以这种方式接近它 -

HTML:

  1. 我在每个页面中都有一个divwithcontent类,其中包含该页面的内容。页眉和页脚是分开的。
  2. 我为每个页面添加了一个独特的类content。例如,如果我正在创建一个CONTACT US页面,我会将页面的内容放入<section class="content contact-us"></section>.
  3. 这样,我可以更轻松地在单个style.css中编写特定于页面的 CSS 。

<body>
    <header>
        <div class="nav-menu">
            <ul class="parent-nav">
                <li><a href="#">Home</a></li>
                <li><a href="#">Contact us</a></li>
                ...
            </ul>
        </div>
    </header>

    <section class="content contact-us">
        Content for contact us page goes here
    </section>

    <footer> ... </footer>

</body>

CSS:

  1. 我已经定义了一个active类,它包含活动菜单的样式。

.active {
    color: red;
    text-decoration: none;
}
<body>
    <header>
        <div class="nav-menu">
            <ul class="parent-nav">
                <li><a href="#">Home</a></li>
                <li><a href="#">Contact us</a></li>
                ...
            </ul>
        </div>
    </header>

    <section class="content contact-us">
        Content for contact us page goes here
    </section>

    <footer> ... </footer>

</body>

JavaScript:

  1. 现在在 JavaScript 中,我的目标是将菜单链接文本与 HTML 中定义的唯一类名进行比较。我正在使用 jQuery。
  2. 首先,我获取了所有菜单文本,并执行了一些字符串函数以使文本小写并用连字符替换空格,使其与类名匹配。
  3. 现在,如果content该类与菜单文本具有相同的类(小写且没有空格),则将active类添加到菜单项。

var $allMenu = $('.nav-menu > .parent-nav > li > a');
var $currentContent = $('.content');
$allMenu.each(function() {
  $singleMenuTitle = $(this).text().replace(/\s+/g, '-').toLowerCase();
  if ($currentContent.hasClass($singleMenuTitle)) {
    $(this).addClass('active');
  }
});
.active {
  color: red;
  text-decoration: none;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<body>
  <header>
    <div class="nav-menu">
      <ul class="parent-nav">
        <li><a href="#">Home</a></li>
        <li><a href="#">Contact us</a></li>
        ...
      </ul>
    </div>
  </header>

  <section class="content contact-us">
    Content for contact us page goes here
  </section>

  <footer> ... </footer>

</body>

为什么我接近这个?

  1. @Sampson 的回答对我来说效果很好,但我注意到每次我想添加新页面时都必须添加新代码。
  2. 同样在我的项目中,body标签在header.php文件中,这意味着我不能为每个页面编写唯一的类名。
于 2020-10-07T07:52:40.713 回答