2013-06-20 36 views
0

這是我的javascript代碼:JavaScript的表格功能如何主動類添加到激活鍵

<script type="text/javascript"> 
    var showcont = []; 
    var showcont_containers = []; 

    $('#tabs ul li a').each(function() { 

    // note that this only compares the pathname, not the entire url 

    // which actually may be required for a more terse solution. 

    if (this.pathname == window.location.pathname) { 
      showcont.push(this); 
      showcont_containers.push($(this.hash).get(0)); 
     }; 

    }); 

    $(showcont).click(function(e){ 
    $(showcont_containers).hide().filter(this.hash).fadeIn(); 
     e.preventDefault(); 
    }); 
    </script> 

如何主動類添加到#tabs ul li a?請幫助我

+0

這不是一個JavaScript的問題,標記問題的答案是 –

+0

這是代碼.. –

回答

0

如果您只想添加或刪除班級,您可以使用$(this).addClass('active')$(this).removeClass('active')。假設if語句正在代碼中工作,下面的代碼應該工作。

if (this.pathname == window.location.pathname) { 
    showcont.push(this); 
    showcont_containers.push($(this.hash).get(0)); 
    this.addClass('active'); 
}; 

用下面的代碼,您可以添加「活動」類單擊<a>元素,並從兄弟姐妹中刪除。

$('#tabs ul li a').on('click', function() 
{ 
    $(this).addClass('active').siblings().removeClass('active'); 
}); 

這個,如果你每<a>元素點擊後刷新頁面但將無法正常工作。

+0

無法正常工作。你能否將任何課程添加到現有的功能中,而不是採用新的功能? –

+0

編輯了答案,添加了if語句代碼。檢查是否按預期工作。 – Puuskis