我想知道如何添加在您当前所在页面的导航栏中添加突出显示框的功能。你知道这样人们就知道他们在哪个页面上。
非常喜欢这个网站,事件页面上有白框:https://woodycraft.net/home/
这是我的导航栏的 CSS:
/*TOP NAV BAR SECTION*/
*{margin: 0px;
padding: 0px;}
#nav_bar {background-color: #a22b2f;
box-shadow: 0px 2px 10px;
height: 45px;
text-align: center;}
#nav_bar > ul > li {display: inline-block;}
#nav_bar ul > li > a {color: white;
display: block;
text-decoration: none;
font-weight: bold;
padding-left: 10px;
padding-right: 10px;
line-height: 45px;}
#nav_bar ul li ul {display: none;
list-style: none;
position: absolute;
background: #e2e2e2;
box-shadow: 0px 2px 10px;
text-align: left;}
#nav_bar ul li a:hover {background: #8e262a;}
#nav_bar a:active {background: white;}
#nav_bar ul li:hover ul {display: block;}
#nav_bar ul li ul li a {color: #252525;
display: block;}
#nav_bar ul li ul li a:hover {background: #4485f5;
color: #fff;}
这是我的一个页面的 HTML:
<!--TOP NAV BAR SECTION-->
<div id="nav_bar">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="status.html">Status</a></li>
<li><a href="info.html">Info</a></li>
<li><a href="#">Gamemodes</a>
<ul>
<li><a href="survival.html">Survival</a></li>
<li><a href="pure-pvp.html">Pure-PVP</a></li>
<li><a href="gamesworld.html">Gamesworld</a></li>
</ul>
</li>
<li><a href="rules.html">Rules</a></li>
<li><a href="vote.html">Vote</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</div>
最佳答案
您可以使用 jquery 函数 window.location.href 将您现在所在的网站与您的 href 进行比较 在列表元素中。例如,“index.html”:
<li><a href="index.html">Home</a></li>
下面的代码在列表元素 中搜索事件页面的 href。然后使用 addClass('active') 将类“active”添加到事件页面 以便您现在可以通过 CSS 调用它>。将这段代码放在 html 文件的头部。
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
$(function(){
$('a').each(function(){
if ($(this).prop('href') == window.location.href) {
$(this).addClass('active'); $(this).parents('li').addClass('active');
}
});
});
</script>
您现在可以添加您的 css 条件,例如更改颜色:
#nav_bar .active {
color: #F8F8F8;
background-color: #4f81bd;
}
关于html - 当前页面的导航栏突出显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26819675/