Jquery UI Tabs: How do i hide a tab and its corres

2020-03-20 17:04发布

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 its div are removed. I need to just hide the tab and div and when i click Add Tab i should just show the hidden tab and div. I am not sure how to show/hide the tab and div property.

Thanks in advance.

Jeevi

7条回答
再贱就再见
2楼-- · 2020-03-20 17:42

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();
查看更多
干净又极端
3楼-- · 2020-03-20 17:44

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.

查看更多
趁早两清
4楼-- · 2020-03-20 17:48

$(".selector ul li:eq("+index+")").hide();

查看更多
你好瞎i
5楼-- · 2020-03-20 17:48

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

查看更多
6楼-- · 2020-03-20 17:52

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); 
查看更多
劳资没心,怎么记你
7楼-- · 2020-03-20 17:54

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; }
});
查看更多
登录 后发表回答