0

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

<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>

脚本:

$('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();
    });
});

我想修改此脚本以将“活动”类添加/删除lia. 我尝试用 替换$active.addClass('active');$active.parent('li').addClass('active');但结果代码的行为不好。

4

2 回答 2

2

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

// Make the old tab inactive.
$active.parent('li').removeClass('active');

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

// If no match is found, use the first link as the initial active tab.
$active.parent('li').addClass('active');
于 2012-11-09T10:51:06.267 回答
1

有几个地方需要定位父 li:

$('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/

于 2012-11-09T10:53:46.543 回答