Div没有隐藏在标签中

时间:2015-06-11 02:14:46

标签: javascript html css

我已经建立了一个网站,该网站使用Javascript来隐藏某些div并仅在打开选项卡时显示它们。

我的问题是我的一个div出现在所有标签中,我不知道问题是什么。

烦人的DIV:

<div class="tabContent" id="history">
 <h2>History</h2>
 <div>

 </div>

用于显示div的JavaScript代码:

 function showTab() {
  var selectedId = getHash( this.getAttribute('href') );

  // Highlight the selected tab, and dim all others.
  // Also show the selected content div, and hide all others.
  for ( var id in contentDivs ) {
    if ( id == selectedId ) {
      tabLinks[id].className = 'selected';
      contentDivs[id].className = 'tabContent';
    } else {
      tabLinks[id].className = '';
      contentDivs[id].className = 'tabContent hide';
    }
  }

  // Stop the browser following the link
  return false;
}

1 个答案:

答案 0 :(得分:0)

使用 display:none; 更改设置元素仅适用于 display:none; 使用内联进行设置。

所以你不能用class来隐藏/显示元素。

以下可用代码。

<div class="tabContent" id="history" style="display:none;">
    <h2>History</h2>
    <div>

    </div>
</div>

function showTab() {
  var selectedId = getHash( this.getAttribute('href') );

  // Highlight the selected tab, and dim all others.
  // Also show the selected content div, and hide all others.
  for ( var id in contentDivs ) {
    if ( id == selectedId ) {
      tabLinks[id].className = 'selected';
      contentDivs[id].style.display = '';
    } else {
      tabLinks[id].className = '';
      contentDivs[id].style.display = 'none';
    }
  }

  // Stop the browser following the link
  return false;
}
相关问题