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

javascript - How to hide other tabs's content and display only the selected tab's content - Stack Overflow

programmeradmin1浏览0评论

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>

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>

Share Improve this question edited Aug 17, 2019 at 17:04 mplungjan 178k28 gold badges180 silver badges240 bronze badges asked May 9, 2013 at 7:43 user1223844user1223844 871 gold badge2 silver badges7 bronze badges 4
  • 1 jqueryui.com/tabs – DDK Commented May 9, 2013 at 7:48
  • Could you add the HTML for the tab content please? – andyb Commented May 9, 2013 at 7:51
  • Did you set id for your tab contents? – MahanGM Commented May 9, 2013 at 7:56
  • yeah all my tab contens have Id's – user1223844 Commented May 9, 2013 at 7:56
Add a comment  | 

5 Answers 5

Reset to default 8

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

.tabContent {
    display:none;
}

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

<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.

The only caveat is that getElementsByClassName() is not available in IE8 or below. Other (modern) browsers have this function but there are alternatives - see getElementsByClassName & IE8: Object doesn't support this property or method.

You have to first set a display: none for all of your tab contents.

e. g.

<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.

you can try this

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

or the other way

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>
发布评论

评论列表(0)

  1. 暂无评论