开发者

Set a certain tab as active on load with custom jquery tabs

开发者 https://www.devze.com 2023-03-05 21:30 出处:网络
I\'m using http://jqueryfordesigners.com/jquery-tabs/ for a project, the tabbing javascript code looks like this:

I'm using http://jqueryfordesigners.com/jquery-tabs/ for a project, the tabbing javascript code looks like this:

<script type="text/javascript" charset="utf-8">
            $(function () {
                    var tabContainers = $('div.tabs > div');
                    tabContainers.hide().filter(':first').show();

                    $('div.tabs ul.tabNavigation a').click(function () {
                            tabContainers.hide();
                            tabContainers.filter(this.hash).show();
                            $('div.tabs ul.tabNavigation a').removeClass('selected');
                            $(this).addClass('selected');
                            return false;
                    }).filter(':first').click();
            });
    </script>    

and the tabs html looks like this :

<div class="tabs">
    <ul class="tabNavigation">
        <li><a href="#first">First</a></li>
        <li><a href="#second">Second</a></li>
        <li><a href="#third">Third</a></li>
    </ul>

    <div id="first">
        <h2>First</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
    <div id="second">

        <h2>Second</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>

    </div>
    <div id="third">
        <h2>Third</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excep开发者_如何学Goteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    </div>
</div>  

The question is : How do I set a second tab to be selected and shown by default when page loads? I've tried to replace all instances of "first" with "second" in javascript code but it wouldnt work...

Here is a demo page completed with css for you to toy around http://jqueryfordesigners.com/demo/tabs.html :)

Please help!


There are two parts that are selecting the first tab and thus need to change:

tabContainers.hide().filter(':first').show();

and

.filter(':first').click();

The filters are using :first, a selector that selects the first matching element. :second is not a valid selector. To select the second tab instead, try this:

var tabContainers = $('div.tabs > div');
tabContainers.hide(); // don't need to show a tab here; it'll be shown below

$('div.tabs ul.tabNavigation a').click(function () {
    tabContainers.hide();
    tabContainers.filter(this.hash).show();
    $('div.tabs ul.tabNavigation a').removeClass('selected');
    $(this).addClass('selected');
    return false;
}).filter('[href="#second"]').click(); // click second link, based on href attr

This could be cleaned up further, but hopefully it gets you started. You can read up on the jQuery filter() method at http://api.jquery.com/filter/ and the jQuery click() method at http://api.jquery.com/click/.

0

精彩评论

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