문제

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:

http://jsfiddle.net/3e2MW/1/

도움이 되었습니까?

해결책

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;});

Working Demo

다른 팁

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;
         });
 });

Demo

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top