2015-11-03 57 views
0

如何從當前標籤中刪除活動類並將活動類設置爲另一個點擊?我儘量做到這樣,但它不工作:)使用jquery刪除標籤上的所有類,並添加新的

$(function() { 
 
    $(".bir").click(function() { 
 
    // remove classes from all 
 
    $("a").removeClass("active"); 
 
    // add class to the one we clicked 
 
    $(".bir").addClass("active"); 
 

 
    }); 
 
});
a { 
 
    color: #2a6496; 
 
} 
 
a.janduu { 
 
    color: #d11250; 
 
}
<a href="form.php?lang=kr" data-toggle="tooltip" data-placement="bottom" title="кыргызча" class="janduu bir">КЫР</a> 
 
<span>|</span> 
 
<a href="form.php?lang=tr" data-toggle="tooltip" data-placement="bottom" title="türkçe" class="bir">TUR</a> 
 
<span>|</span> 
 
<a href="form.php?lang=en" data-toggle="tooltip" data-placement="bottom" title="english" class="bir">ENG</a> 
 
<span>|</span> 
 
<a href="form.php?lang=ru" data-toggle="tooltip" data-placement="bottom" title="русский" class="bir">РУС</a>

+1

要刪除使用'$(」 a.active「)。removeClass(」active「);'並使用'$(this)'將它添加到clicked元素上。 '$(this).addClass('active');' – Tushar

+0

看起來好像點擊鏈接後,頁面會刷新。頁面刷新瀏覽器將重置動態添加的類「活動」。在這種情況下。後端語言將幫助你。 – Sachink

+0

Tushar你建議使用雙$(this) –

回答

0
$(function() { 
    $(".bir").click(function (e) { 
     e.preventDefault(); 
     // remove classes from all 
     if(!$(this).hasClass("janduu")){ 
      $(this).addClass("janduu"); 
      $(this).siblings().removeClass("janduu"); 
     } 
     // add class to the one we clicked 


    }); 
}); 

嘗試這種方式

demo

+0

親愛的佩卡,你的解決方案是完美的,不幸的是,在使用你的解決方案後,我的鏈接不起作用。 –

+0

@ГаухарТаалайкызы刪除'e.preventDefault();'我把它放在那裏用於測試目的。它會使鏈接不起作用。 – guradio

+0

我已經做到了,但問題是,當我點擊鏈接並加載頁面時,位於當前頁面上的標籤的顏色僅在點擊時發生改變。 –