You are using $(this)
to bind the click event handler which is wrong.Use this:
$('#nav ul li a').click(function() {
$("#nav ul li a").removeClass("active");
$(this).addClass('active');
return false;});
Domanda
I have a menu in which different buttons are placed. When the initial page is loaded, one button is active (declared via class="active
). Now, when I click on a different button, the class will be removed and added to the just clicked button. The code worked well, then I tried something different in jquery but I removed it, but my code isn't working, even though I have a different project in which I have the same code and it's working.
Here's my Jsfiddle:
Soluzione
You are using $(this)
to bind the click event handler which is wrong.Use this:
$('#nav ul li a').click(function() {
$("#nav ul li a").removeClass("active");
$(this).addClass('active');
return false;});
Altri suggerimenti
change the code like this:
$(document).ready(function() {
$("#nav ul li a").click(function(e) {
e.preventDefault();
$("#nav ul li a").removeClass("active");
$(this).addClass('active');
return false;
});
});