开发者

Showing multiple elements onClick

开发者 https://www.devze.com 2023-01-03 17:26 出处:网络
HTML: <ul id=\"mode\"> <li><a href=\"#tab1\">tab1</a> <div class=\"extra\">tada</div>

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 here: http://jsfiddle.net/wwMJL/

I'd like to also show each li's 'extra' div on click and hide when the tab is inactive, what do I need to change in the code?

Thanks!


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

0

精彩评论

暂无评论...
验证码 换一张
取 消