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添加活动类?

如果您只想添加或删除类,您可以使用$(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');
};

使用下面的代码,您可以将"active"类添加到您单击的<a>元素,并将其从兄弟元素中删除。

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

如果你在每次点击<a>元素后刷新页面,这将不起作用。

最新更新