JavaScript show/hide tab

后端 未结 3 1384
悲&欢浪女
悲&欢浪女 2021-01-20 07:08

What i want is hide all content first, then click one tab, the corresponding content shows (the tab becomes \'active\'), where click it again it will disappear. some of the

相关标签:
3条回答
  • 2021-01-20 07:35

    Change the click function to:

    $('#nav ul li a').click(function(){ 
        var currentTab = $(this).attr('href');  
        $('#nav div').not(currentTab).hide();
        $('#nav ul li').removeClass('active');
        $(this).parent().toggleClass('active');     
        if (currentTab.indexOf("mailto:") === -1)
        {
            $(currentTab).toggle();
        }
    });
    

    Working example

    0 讨论(0)
  • 2021-01-20 07:44
       $(document).ready(function(){
    $('#nav div').hide();
    $('#nav div:first').show();
    $('a').click(function(){    
        var currentTab = '#'+$(this).text().toLowerCase();
        if($(currentTab).is(':visible')){
            $('#nav div').hide();
            $(currentTab).hide();
        }else{
            $('#nav div').hide();
            $(currentTab).show();
        }
    }
    );
    });
    
    
    <div id="nav">
        <ul>
          <li><a href="#">About</a></li>
          <li><a href="#">Email</a></li>
          <li><a href="#">Contact</a></li>
        </ul>
        <div id="about">
          about
        </div>
        <div id="contact">
          contact
        </div>
    </div>
    

    Please try above code.. I change href value to just "#" then some change in jquery. Please ask in comment if you have any doubt.. click here to test

    0 讨论(0)
  • 2021-01-20 07:56

    This should work:

    $(function() {
        $('#nav div').hide();
        $('#nav div:first').show();
        $('#nav ul li:first').addClass('active');
        $('#nav ul li a').click(function(){
            var currentTab = $(this).attr('href');
            var vis = $(currentTab).is(':visible');
            $('#nav div').hide();
            $('#nav ul li').removeClass('active');
            $(this).parent().addClass('active');
            if(vis) {
                $(currentTab).hide();
            } else {
                $(currentTab).show();
            }
        });
    });
    

    you have to check if the current tab is visible before you hide it.

    working: http://jsfiddle.net/tqhHA/

    0 讨论(0)
提交回复
热议问题