最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Switch between tabs without reloading - Stack Overflow

programmeradmin9浏览0评论

I have two tabs in the content: Comerciales - Legales

Each of these tabs should display a list of files, like the ones that are now displayed.

I want to make that if the user clicks on Legales it hides the files of Comerciales tab and the tab bees active (getting the active class and removing it from Comerciales) and displaying only the files that belong to Legales.

I have tried with hide and fade in but I couldn't get the active class to the current tab. Can someone help me out?

Here it's my markup:

        <ul class="pdf_documents clearfix">
            <li class="active"><a href="javascript:void(0)">Comerciales</a></li>
            <li><a href="javascript:void(0)">Legales</a></li>
        </ul>
        <div class="pdf_box">
            <div class="pdf_file">
                <img src="images/pdf.png" alt="pdf" width="70" height="70">
                <p>Ficha Técnica</p>
            </div>
            <div class="pdf_file">
                <img src="images/pdf.png" alt="pdf" width="70" height="70">
                <p>Ficha Descriptiva</p>
            </div>
            <div class="pdf_file">
                <img src="images/pdf.png" alt="pdf" width="70" height="70">
                <p>Último informe</p>
            </div>
            <div class="pdf_file">
                <img src="images/pdf.png" alt="pdf" width="70" height="70">
                <p>Informes anteriores</p>
            </div>
            <div class="pdf_file">
                <img src="images/pdf.png" alt="pdf" width="70" height="70">
                <p>Folleto pleto</p>
            </div>
        </div>

I have two tabs in the content: Comerciales - Legales

Each of these tabs should display a list of files, like the ones that are now displayed.

I want to make that if the user clicks on Legales it hides the files of Comerciales tab and the tab bees active (getting the active class and removing it from Comerciales) and displaying only the files that belong to Legales.

I have tried with hide and fade in but I couldn't get the active class to the current tab. Can someone help me out?

Here it's my markup:

        <ul class="pdf_documents clearfix">
            <li class="active"><a href="javascript:void(0)">Comerciales</a></li>
            <li><a href="javascript:void(0)">Legales</a></li>
        </ul>
        <div class="pdf_box">
            <div class="pdf_file">
                <img src="images/pdf.png" alt="pdf" width="70" height="70">
                <p>Ficha Técnica</p>
            </div>
            <div class="pdf_file">
                <img src="images/pdf.png" alt="pdf" width="70" height="70">
                <p>Ficha Descriptiva</p>
            </div>
            <div class="pdf_file">
                <img src="images/pdf.png" alt="pdf" width="70" height="70">
                <p>Último informe</p>
            </div>
            <div class="pdf_file">
                <img src="images/pdf.png" alt="pdf" width="70" height="70">
                <p>Informes anteriores</p>
            </div>
            <div class="pdf_file">
                <img src="images/pdf.png" alt="pdf" width="70" height="70">
                <p>Folleto pleto</p>
            </div>
        </div>
Share Improve this question edited Nov 5, 2012 at 10:29 asked Nov 5, 2012 at 9:02 user1617218user1617218 1
  • 1 You can use jquery functions: addClass + removeClass or toggleClass .. to remove and add the active class to the correct tab. – Jozzeh Commented Nov 5, 2012 at 9:25
Add a ment  | 

3 Answers 3

Reset to default 3

See this fiddle: http://jsfiddle/MrZdh/

I've added some more classes to your markup so that it is easier to add jQuery handlers.

    <ul class="pdf_documents clearfix">
        <li class="tab- active"><a href="javascript:void(0)">Comerciales</a></li>
        <li class="tab-leg"><a href="javascript:void(0)">Legales</a></li>
    </ul>
    <div class="pdf_box">
        <div class="pdf_file erciales">
            <img src="images/pdf.png" alt="pdf" width="70" height="70">
            <p>Ficha Técnica</p>
        </div>
        <div class="pdf_file legales">
            <img src="images/pdf.png" alt="pdf" width="70" height="70">
            <p>Ficha Descriptiva</p>
        </div>
        <div class="pdf_file erciales">
            <img src="images/pdf.png" alt="pdf" width="70" height="70">
            <p>Último informe</p>
        </div>
        <div class="pdf_file legales">
            <img src="images/pdf.png" alt="pdf" width="70" height="70">
            <p>Informes anteriores</p>
        </div>
        <div class="pdf_file erciales">
            <img src="images/pdf.png" alt="pdf" width="70" height="70">
            <p>Folleto pleto</p>
        </div>
    </div>

Legales content (marked by corresponding class) is hidden via display:none by default.

Then tab click handlers can look like this:

    $('.tab- a').click(function(e){
        //make all tabs inactive
        $('.pdf_documents a').removeClass('active');
        //then make the clicked tab active
        $(this).addClass('active');    
        $('.pdf_box .legales').hide();
        $('.pdf_box .erciales').show();
    });


    $('.tab-leg a').click(function(e){
        //make all tabs inactive    
        $('.pdf_documents a').removeClass('active');
        //then make the clicked tab active
        $(this).addClass('active');
        $('.pdf_box .erciales').hide();
        $('.pdf_box .legales').show();
    });

Or yes, use some tabs plugin from kalpesh patel's answer.

As you have not posted your JS code, I don't know where you are wrong:

I would suggest that, you can use, jqueryui's tab:

http://jqueryui./tabs/

There are numerous example demonstrating how to implement tabs. Google it.

Few more resources:

http://www.jacklmoore./notes/jquery-tabs

http://jqueryfordesigners./jquery-tabs/

http://www.apricot-studios./lab/jquery/jquery-tabs-tutorial.php

I you are willing to use some already existing plug-in than try this one, you can easily get it working the way you want plus if you want to learn the tricks you can also play around with the code in it.

Hope it helps.

jquery-content-panel-switcher

发布评论

评论列表(0)

  1. 暂无评论