2016-03-12 119 views
0

我不知道爲什麼我的標籤無法正常工作 我一直在使用它幾個小時了! https://jsfiddle.net/mshgwjrb/我的標籤無法正常工作

$('.tabs').each(function() { 
    var $this = $(this); 
    var $active = $this.find('li .active'); 
    var $link = $active.find('a'); 
    var $activeTab = $($link.attr('href')); 

    $this.on('click', '.tab-', function (e) { 
     e.preventDefault(); 
     var currLink = $(this); 
     var id = this.hash; 

     if (!currLink.is('.active')) { 
      $activeTab.removeClass('active'); 
      $active.removeClass('active'); 

      $(id).addClass('active'); 
      currLink.parent().addClass('active'); 
     } 
    }); 
}); 
+0

有一個關於'李.active'一個錯字,你想'li.active'針對任何LI與'.active'類 – adeneo

+0

你有什麼建議? –

+0

S/he建議您將您的拼寫錯誤從'li .active'修復爲'li.active' – 8protons

回答

0

使用此代碼。比較你所擁有的代碼和我在這裏給你的代碼。你會理解這個問題。

$('.tabs').each(function(){ 
    var $this = $(this); 
    var $active = $this.find('li .active'); 
    var $link = $active.find('a'); 
    var $activeTab = $($link.attr('href')); 



    $this.on('click', '.tab-', function(e){ 
    alert("asdf") 
     //e.preventDefault(); 
     var currLink = $(this); 
     var id = this.hash; 
     $active = $("#tabsContent").find('.active'); 
     $activeTab.removeClass('active'); 
      $active.removeClass('active'); 

      $(id).addClass('active'); 

    }); 


}); 
0

your fiddle你必須在你的HTML,所有的標籤ID的使用相同的ID錯誤。另外你的運行你的JQuery的方法不是最乾淨的。我已經爲你重寫你的例子HERE。新的jQuery代碼看起來是這樣的:

$('ul.tabs').on('click', 'li', function(e) { 
    var id = $(this).children("a").attr('href'); 
    $('.tab.active').removeClass('active'); 
    $(id).addClass('active'); 
});