When clicked the active class is left with the original "li" where it should have changed. I tried messing with the code but was unable to find a solution. can someone please check what I missed. I think I am mssing something specific to my code. I tried "add/remove active class for ul list with jquery?" but that did not help. If you can find the fault in my code it would be great.
Html:
<div class="collapse navbar-collapse navbar-ex1-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="#juk">About</a></li>
<li><a onclick="myFunction1()">Development</a></li>
<li><a href="http://docs.kde.org/stable/en/kdemultimedia/juk/index.html">Wiki/Documentation</a></li>
<li><a onclick="myFunction()">Screenshots</a></li>
<li class="dropdown">
<a data-toggle="dropdown" class="dropdown-toggle" href="#">Releases </a>
<ul class="dropdown-menu">
<li><a href="ftp://ftp.kde.org/pub/kde/stable/latest">JuK Latest</a></li>
<li><a href="http://developer.kde.org/~wheeler/files/src/juk-1.95a.tar.gz">JuK 1.95</a></li>
<li><a href="http://developer.kde.org/~wheeler/files/src/juk-1.1.tar.gz">JuK 1.1</a></li>
<li><a href="http://developer.kde.org/~wheeler/files/src/juk-1.0-1.tar.gz">JuK 1.0</a></li>
</ul>
</li>
<li><a href="#contactus">Contact Us</a></li>
</ul>
</div>
JS:
$(function() {
( 'ul.nav.navbar-nav.navbar-right li' ).on( 'click', function() {
$( this ).parent().find( 'li.active' ).removeClass( 'active' );
$( this ).addClass( 'active' );
});
});
You are targeting the wrong <li>
element in the .on('click')
event
You said
$( 'ul.nav navbar-nav navbar-right li' )
Which will look for li
that is a child of navbar-right
that is also a child of navbar-nav
that is also a child of ul.nva
.
What you should have targeted is:
$( 'ul.nav.navbar-nav.navbar-right li' )
Notice the space before the li
? that is because it is a descendant of the ul
element that has multiple classes.
I know you copied the class list from the HTML DOM element, but you have to avoid spaces in jQuery selector if you are talking about a single element that has multiple classes.
$(function() { $('ul.nav.navbar-nav.navbar-right li').on('click', function() { $(this).parent().find('li.active').removeClass('active'); $(this).addClass('active'); }); });
.active { background-color: gold; }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> <div class="collapse navbar-collapse navbar-ex1-collapse"> <ul class="nav navbar-nav navbar-right"> <li class="active"><a href="#juk">About</a></li> <li><a>Development</a></li> <li><a href="#">Wiki/Documentation</a></li> <li><a >Screenshots</a></li> <li class="dropdown"> <a data-toggle="dropdown" class="dropdown-toggle" href="#">Releases </a> <ul class="dropdown-menu"> <li><a href="#">JuK Latest</a></li> <li><a href="#">JuK 1.95</a></li> <li><a href="#">JuK 1.1</a></li> <li><a href="#">JuK 1.0</a></li> </ul> </li> <li><a href="#contactus">Contact Us</a></li> </ul> </div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.