Jquery UI Tabs: How do i hide a tab and its corresponding div when i close it

后端 未结 7 1072
隐瞒了意图╮
隐瞒了意图╮ 2021-01-13 00:30

I have used Jquery UI Tabs, and given close option to the tabs. By default i am creating three tabs and its corresponding three divs. Now when i close a tab then the tab and

相关标签:
7条回答
  • 2021-01-13 00:57

    Not 100% sure on the specific code, but try something like this to hide-and-not-delete the tab:

    $( ".selector" ).tabs({
        remove: function(event, ui) { $(this).hide(); return false; }
    });
    
    0 讨论(0)
  • 2021-01-13 00:58

    Here is another and I believe, more simple solution - simply hide li tags. In my case the tabs have 'data-carrier-id' class:

    var tabs = $("li[data-carrier-id]");
    tabs.hide();
    

    Then you can show a particular tab:

    $("li[data-carrier-id=" + carrierId + "]").show();
    

    Hiding and showing the tabs hides and shows corresponding divs.

    Here is one wrinkle. After changing the tab visibility, the selected tab has to be changed. This is by design. Even with "option" "disable" the selected tab cannot be disabled. This is relatively easy to fix, just loop through the visible tabs and find the first visible index:

    var firstVisibleTabIndex;
    tabs.each(function (index) {
      if ($(this).is(":visible")) {
         firstVisibleTabIndex = index;
         return false;
       }
     });
     var jqTabs = $("#tabs").tabs();
     jqTabs.tabs("option", "active", firstVisibleTabIndex); 
    
    0 讨论(0)
  • 2021-01-13 01:01

    I just tested this for two tabs, you can add the needed logic to make it available for N tabs.

    For this you open a first tab by default, then you open a second tab then:

    $("#yourTabHref").parent().children(":first").children(":first").next().hide();
    

    Explanation: The parent is used to go to the div of your tabs, then children(":first") moves you tho the ul, then again children(":first") moves you to the first li, but we are going to hide the second tab, that means the second li that's why whe use the next(), now we are at the second tab, then just hide it.

    By last, just hide the tab content:

    $("#yourTabHref").hide();
    

    To show everything again just:

    $("#yourTabHref").parent().children(":first").children(":first").next().show();
    $("#yourTabHref").hide();
    
    0 讨论(0)
  • 2021-01-13 01:05

    Based on the discussion at http://old.nabble.com/UI-Tabs:-How-to-hide-show-a-Tab-td19569388s27240.html the following has worked for me -

    Adding the following CSS

    .ui-tabs .ui-state-disabled { 
        display: none; /* disabled tabs don't show up */ 
    } 
    

    and then using the tabs disabled options in either of the following forms -

    $( ".selector" ).tabs({ disabled: [1, 2] }); //when initializing the tabs
    $( ".selector" ).tabs( "option", "disabled", [1, 2] ); // or setting after init
    

    see http://jqueryui.com/demos/tabs/#option-disabled for the detailed jQuery docs

    0 讨论(0)
  • 2021-01-13 01:05

    Well it may not be too late to answer this query. What I did is to give an id to html li

    <div id="tabs">
      <ul>
        <li><a href="#tabs-1">New Item</a></li>
        <li><a href="#tabs-2">Product</a></li>
        <li><a href="#tabs-3">Purchase Order</a></li>
        <li><a href="#tabs-4">Administration</a></li>
        <li><a href="#tabs-5">License</a></li>
        <li **id="tab-6"**><a href="#tabs-6">Test</a></li>
        <li><a href="#tabs-7">Specific Product</a></li>
        <li><a href="#tabs-8">Support</a></li>
      </ul>
    

    then I used JQuery code $('#tab-6').hide(); to hide and $('#tab-6').show(); to Show the tab.

    Hope this helps Cheers

    0 讨论(0)
  • 2021-01-13 01:06

    This bugged me for a while too and I ended up writing a small plugin to make it easy. You can check it out here: KylesTechnobabble (with a JSFiddle example).

    Note: This is for jQuery UI 1.9.2. I haven't tested with anything else.

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