首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在jQuery选项卡脚本中更改元素父元素的类?

如何在jQuery选项卡脚本中更改元素父元素的类?
EN

Stack Overflow用户
提问于 2012-11-09 18:31:55
回答 2查看 490关注 0票数 0

我使用本教程中的代码:jacklmoore.com/note/jquery-tabs。

代码语言:javascript
复制
<ul class='tabs'>
    <li><a href='#tab1'>Tab 1</a></li>
    <li><a href='#tab2'>Tab 2</a></li>
    <li><a href='#tab3'>Tab 3</a></li>
</ul>
<div id='tab1'>
    <p>Hi, this is the first tab.</p>
</div>
<div id='tab2'>
    <p>This is the 2nd tab.</p>
</div>
<div id='tab3'>
    <p>And this is the 3rd tab.</p>
</div>

脚本:

代码语言:javascript
复制
$('ul.tabs').each(function(){
    // For each set of tabs, we want to keep track of
    // which tab is active and it's associated content
    var $active, $content, $links = $(this).find('a');

    // If the location.hash matches one of the links, use that as the active tab.
    // If no match is found, use the first link as the initial active tab.
    $active = $($links.filter('[href="'+location.hash+'"]')[0] || $links[0]);
    $active.addClass('active');
    $content = $($active.attr('href'));

    // Hide the remaining content
    $links.not($active).each(function () {
        $($(this).attr('href')).hide();
    });

    // Bind the click event handler
    $(this).on('click', 'a', function(e){
        // Make the old tab inactive.
        $active.removeClass('active');
        $content.hide();

        // Update the variables with the new link and content
        $active = $(this);
        $content = $($(this).attr('href'));

        // Make the tab active.
        $active.addClass('active');
        $content.show();

        // Prevent the anchor's default click action
        e.preventDefault();
    });
});

我想修改这个脚本,以便在a的父li元素中添加/删除“active”类。我尝试用$active.parent('li').addClass('active');替换$active.addClass('active');,但结果代码的行为并不好。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-11-09 18:51:06

您只需要从之前单击的元素中删除active类:

代码语言:javascript
复制
// Make the old tab inactive.
$active.parent('li').removeClass('active');

您还需要向第一个list元素添加active

代码语言:javascript
复制
// If no match is found, use the first link as the initial active tab.
$active.parent('li').addClass('active');
票数 2
EN

Stack Overflow用户

发布于 2012-11-09 18:53:47

有几个地方需要针对父li:

代码语言:javascript
复制
$('ul.tabs').each(function(){
    // For each set of tabs, we want to keep track of
    // which tab is active and it's associated content
    var $active, $content, $links = $(this).find('a');

    // If the location.hash matches one of the links, use that as the active tab.
    // If no match is found, use the first link as the initial active tab.
    $active = $($links.filter('[href="'+location.hash+'"]')[0] || $links[0]);
    $active.parent('li').addClass('active');
    $content = $($active.attr('href'));

    // Hide the remaining content
    $links.parent('li').not($active).each(function () {
        $($(this).attr('href')).hide();
    });

    // Bind the click event handler
    $(this).on('click', 'a', function(e){
        // Make the old tab inactive.
        $active.parent('li').removeClass('active');
        $content.hide();

        // Update the variables with the new link and content
        $active = $(this);
        $content = $($(this).attr('href'));

        // Make the tab active.
        $active.parent('li').addClass('active');
        $content.show();

        // Prevent the anchor's default click action
        e.preventDefault();
    });
});​

我拼凑了一个看起来可以工作的JSFiddle:

http://jsfiddle.net/5kGTX/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13306349

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档