简体   繁体   English

如何隐藏其他选项卡的内容并仅显示所选选项卡的内容

[英]How to hide other tabs's content and display only the selected tab's content

When I click on a particular tab the other tabs's content should be hidden but it is not hiding. 当我单击一个特定的选项卡时,其他选项卡的内容应被隐藏,但不会被隐藏。 This is all the code I have. 这就是我所有的代码。

 function showStuff(id) { if (document.getElementById(id).style.display === "block") { document.getElementById(id).style.display = "none"; } else { document.getElementById(id).style.display = "block"; } } 
 <ul class="side bar tabs"> <li id="tabs1" onclick="showStuff('tabs-1')">City</li> <li id="tabs2" onclick="showStuff('tabs-2')">Country</li> <li id="tabs3" onclick="showStuff('tabs-3')">Humanity</li> </ul> <div id="tabs-1" style="display : none"> <p>Proin elit m</p> </div> <div id="tabs-2" style="display : none"> <p>M massa ut d</p> </div> <div id="tabs-3" style="display : none"> <p> sodales.</p> </div> 

Giving all the tab content elements a common CSS class makes selecting and styling them much easier, for example in this demo and code below. 通过为所有选项卡内容元素提供通用的CSS类,可以更轻松地选择和设置它们的样式,例如在此演示和下面的代码中。

CSS 的CSS

.tabContent {
    display:none;
}

JavaScript 的JavaScript

function showStuff(element)  {
    var tabContents = document.getElementsByClassName('tabContent');
    for (var i = 0; i < tabContents.length; i++) { 
        tabContents[i].style.display = 'none';
    }

    // change tabsX into tabs-X in order to find the correct tab content
    var tabContentIdToShow = element.id.replace(/(\d)/g, '-$1');
    document.getElementById(tabContentIdToShow).style.display = 'block';
}

HTML 的HTML

<ul class="side bar tabs">
    <li id="tabs1" onclick="showStuff(this)">City</li>
    <li id="tabs2" onclick="showStuff(this)">Country</li>
    <li id="tabs3" onclick="showStuff(this)">Humanity</li>  
</ul>

<div id="tabs-1" class="tabContent">
    <p>Proin elit m</p>
</div>
<div id="tabs-2" class="tabContent">
    <p>M massa ut d</p>
</div>
<div id="tabs-3" class="tabContent">
    <p> sodales.</p>
</div>

I have also updated the showElement function to be more generic so that there is less code duplication when hiding and showing the correct tab content. 我还更新了showElement函数,使其更加通用,以便在隐藏和显示正确的选项卡内容时减少重复代码。

The only caveat is that getElementsByClassName() is not available in IE8 or below. 唯一的警告是, getElementsByClassName()在IE8或更低版本中不可用。 Other (modern) browsers have this function but there are alternatives - see getElementsByClassName & IE8: Object doesn't support this property or method . 其他(现代)浏览器具有此功能,但还有其他选择-请参见getElementsByClassName&IE8:对象不支持此属性或方法

You have to first set a display: none for all of your tab contents. 您必须首先设置一个display: none所有选项卡内容display: none

eg 例如

<script type="text/javascript">
function showTab(selected, total)
{
  for(i = 1; i <= total; i += 1)
  {
    document.getElementById('tabs-' + i).style.display = 'none';
  }

  document.getElementById('tabs-' + selected).style.display = 'block';
}
</script>

<div id="tabs-1" style="display: none">tab1</div>
<div id="tabs-2" style="display: none">tab2</div>
<div id="tabs-3" style="display: none">tab3</div>

<ul class="side bar tabs">
  <li id = "tabs1" onclick = "showTab(1,3)">City</li>
  <li id = "tabs2" onclick = "showTab(2,3)">Country</li>
  <li id = "tabs3" onclick = "showTab(3,3)">Humanity</li>  
</ul>

If there is something like this with your code, I'm sure that it's going to work. 如果您的代码中有类似的内容,我敢肯定它会起作用。 BTW, Check your Console window for JavaScript errors. 顺便说一句,检查您的控制台窗口中的JavaScript错误。

I think that this should do it: 我认为应该这样做:

<ul class="side bar tabs">
    <li  id = "tabs1" onclick = "showStuff('tabs-1')">City</li>
    <li  id = "tabs2" onclick = "showStuff('tabs-2')">Country</li>
    <li  id = "tabs3" onclick = "showStuff('tabs-3')">Humanity</li>  
</ul>

  <script type="text/javascript">
  function showStuff (id) 
  {

        document.getElementById("tabs-1").style.display = "none";
        document.getElementById("tabs-2").style.display = "none";
        document.getElementById("tabs-3").style.display = "none";
        document.getElementById(id).style.display = "block";

  }
  </script>

  <div id="tabs-1" style="display:none">tabs 1 content</div>
  <div id="tabs-2" style="display:none">tabs 2 content</div>
  <div id="tabs-3" style="display:none">tabs 3 content</div>
<script>
$(function() {
$( "#tabs" ).tabs();
});
</script>

<div id="tabs">
 <ul>
  <li><a href="#tabs-1">City</a></li>
  <li><a href="#tabs-2">Country</a></li>
  <li><a href="#tabs-3">Humanity</a></li>
 </ul>
 <div id="tabs-1">..............</div>
 <div id="tabs-2">..............</div>
 <div id="tabs-3">..............</div>
</div>

you can try this 你可以试试这个

 function showStuff(id){
$('#tabs1').empty();
  $('#tab2').show();
}

or the other way 或另一种方式

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何显示所选标签的内容并隐藏其他标签的内容? - How to display selected tab content and hide other tab's content? 如何使用JQuery隐藏其他选项卡内容并仅显示选定的选项卡内容? - How to hide other tabs content and display only selected tabs content with JQuery? Javascript:如何显示默认选项卡内容但隐藏其他选项卡内容 - Javascript : How to display a default tab content but hide other tab content 如何隐藏所有角度材质选项卡的选项卡内容 - How to hide tab content of all the angular-material tabs PHP Jquery选项卡:内容加载到一个选项卡的面板中 - PHP Jquery tabs: content loads into one tab's panel 如何使用jQuery UI选项卡确定选项卡的内容何时完成渲染 - How to determine when a tab's content has finished rendering using jQuery UI tabs 多个选项卡列表:如何仅显示单击的选项卡列表的内容? - Multiple tabs lists: How to show only the content of the clicked tab list? 带有onclick的标签会激活其他CSS样式并显示其特定内容 - Tabs with onclick activates other css style AND shows it's specific content angularjs显示/隐藏选项卡,单击相同选项卡隐藏内容 - angularjs show/hide tabs, click same tab hide the content 如何隐藏选项卡的部分或内容 - how to hide the section or content of the tab
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM