2013-06-20 29 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