在onClick上显示多个元素

时间:2010-06-15 08:19:40

标签: jquery toggle tabs

HTML:

<ul id="mode">
    <li><a href="#tab1">tab1</a>
        <div class="extra">tada</div>
        </li>
    <li><a href="#tab2">tab2</a>
        <div class="extra">tada</div> 
    </li>
</ul>
<div id="tab1" class="tab-content" style="display: none">content 1</div>
<div id="tab2" class="tab-content" style="display: none">content 2</div>
​

jQuery的:

$(function(){
    var mode = $('#mode');
    var arrow = $('<span/>', {'class': 'arrow'});
    $('li a', mode).bind('click.mytabs', function() {
        $('li', mode).removeClass('active');
        $(this).parent().addClass('active').append(arrow);
        var a = $(this).attr('href');
        $('.tab-content').hide();
        $(a).show();
        return false;  
    }).filter(':first').triggerHandler('click.mytabs'); // eq(0) works as well
});

JSFiddle:http://jsfiddle.net/wwMJL/

我还希望在标签处于非活动状态时显示每个li的“额外”div,点击并隐藏,我需要在代码中更改哪些内容?

谢谢!

1 个答案:

答案 0 :(得分:1)

$(function(){
    var mode = $('#mode');
    var arrow = $('<span/>', {'class': 'arrow'});
    $('li a', mode).bind('click.mytabs', function() {
        $('li div', mode).hide();
        $(this).siblings('div').show();
        $('li', mode).removeClass('active');
        $(this).parent().addClass('active').append(arrow);
        var a = $(this).attr('href');
        $('.tab-content').hide();
        $(a).show();
        return false;  
    }).filter(':first').triggerHandler('click.mytabs'); // eq(0) works as well
});

demo