我想根據用戶的點擊動態更改列表項的類。我有一個菜單,我想.active
類僅分配給該用戶,最初主菜單點擊必須有效如何使用JS函數動態地更改列表的類
這裏說菜單是我的代碼
function activeclass(id)
{
document.getElementById(id).className += "active";
}
<section id="menu-area">
<nav class="navbar navbar-default" role="navigation">
<div class="container">
<div class="navbar-header">
<!-- FOR MOBILE VIEW COLLAPSED BUTTON -->
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<!-- LOGO -->
<!-- TEXT BASED LOGO <a class="navbar-brand" href="index.html">Inventive</a> -->
<!-- IMG BASED LOGO -->
<a class="navbar-brand" href="index.html"><img class="img-responsive" src="assets/images/logo.png" alt="logo" width="200"></a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul id="top-menu" class="nav navbar-nav navbar-right main-nav">
@yield('logo')
<li id="home"><a href="{{ URL::route('index') }}" onclick="activeclass(id)">Home</a></li>
<li id="aboutus"><a href="{{ URL::route('aboutus') }}" onclick="activeclass(id)">About Us</a></li>
<li id="products"><a href="{{ URL::route('products') }}" onclick="activeclass(id)">Products</a></li>
<li id="gallery"><a href="{{ URL::route('gallery') }}" onclick="activeclass(id)" >Gallery</a></li>
<li id="contactus"><a href="{{ URL::route('contactus') }}" onclick="activeclass(id)">Contact</a></li>
</ul>
</div><!--/.nav-collapse -->
</div>
</nav>
</section>
在CSS文件活動類與li將用於突出顯示選定的頁面菜單
類似'.active {background:red}'?究竟是什麼問題? –
上面是我做的代碼...它不工作..我失去了一些東西..不知道什麼 – nmajethiya