什么阻止了此JS脚本末尾的click事件?

时间:2017-02-09 03:53:15

标签: javascript html css

由于' defaultOpen'我无法看到错误导致Intro标签被设置为加载时的有效标签。 ID。它在点击之前不会改变颜色,它应该看起来那样而不必点击它。这是W3Schools HowTo lesson on Tabs的简单副本,但有些不对。



function openContent(evt, contentType) {
  var i, tabcontent, tabheads;
  tabcontent =
    document.getElementsByClassName('tabcontent');
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = 'none';
  }
  tabheads =
    document.getElementsByClassName('tabheads');
  for (i = 0; i < tabheads.length; i++) {
    tabheads[i].className =
      tabheads[i].className.replace(' active', '')
  }
  document.getElementById(contentType).style.display = 'block';
  evt.currentTarget.className += ' active';
}

document.getElementById('defaultOpen').click();
&#13;
.outerDiv {
  background-color: rgba(50, 50, 0, 0.7);
}
.innerDiv {
  background-color: rgba(10, 10, 10, 0.7);
  color: white;
  display: flex;
  flex-flow: column nowrap;
  align-items: flex-end;
  margin: 25px;
}
.innerDiv p {
  padding: 0 1.2rem;
  margin: 0;
  align-self: center;
}
ul.tabs {
  display: flex;
  align-items: center;
  list-style-type: none;
  margin: 1rem 0 0.3rem 0;
  font-weight: 400;
}
.tabs li a {
  padding: 0 1.2rem 0.5rem 1.2rem;
  background-color: rgba(0, 0, 0, 0.4);
  color: #96912d;
  margin: 0.3rem 0.3rem 0.5rem 0.3rem;
}
.tabs li a:hover {
  background-color: black;
  color: yellow;
}
.tabs li a:focus,
.tabs li a .active {
  background-color: rgba(10, 10, 10, 0.7);
  color: #fac819;
}
.under-tabs {
  margin-top: 0;
}
.tabcontent {
  display: none;
}
&#13;
<section>
  <div class="outerDiv">
    <ul class="tabs">
      <li><a href="javascript:void(0)" class="tabheads" onclick="openContent(event,'intro')" id="defaultOpen">Intro</a>
      </li>
      <li><a href="javascript:void(0)" class="tabheads" onclick="openContent(event,'blog')">Blog</a>
      </li>
      <li><a href="javascript:void(0)" class="tabheads" onclick="openContent(event,'comments')">Comments</a>
      </li>
    </ul>
    <div class="innerDiv under-tabs tabcontent" id="intro">
      <p>
        This project is building a series of virtual colonies on the Moon.
      </p>
    </div>
    <div class="tabcontent" id="blog">
      <iframe src="blogMain.html"></iframe>
    </div>
    <div class="tabcontent" id="comments">
      Comment section!!!
    </div>
  </div>
</section>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

样式中有错误

.tabs li a:focus, .tabs li a.active {
   background-color: rgba(10, 10, 10, 0.7);
   color: #fac819; 
}

在css中,a标记和.active类之间有空格,因为该类附加到a元素