单击活动按钮时关闭选项卡(无 jQuery)

JustChillinInDaHood

我有这个标签代码片段编辑器:

function openLanguage(evt, codeLanguage) {
  var i, tabcontent, tablinks;
  tabcontent = document.getElementsByClassName("tabcontent");
  for (i = 0; i < tabcontent.length; i++) {
    tabcontent[i].style.display = "none";
  }
  tablinks = document.getElementsByClassName("tablinks");
  for (i = 0; i < tablinks.length; i++) {
    tablinks[i].className = tablinks[i].className.replace(" active", "");
  }
  document.getElementById(codeLanguage).style.display = "block";
  evt.currentTarget.className += " active";
}
body {font-family: Arial;}

.tab {
  overflow: hidden;
  border: 1px solid #ccc;
  border-bottom: none;
  background-color: #f1f1f1;
}

.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.1s;
  font-size: 17px;
}

.tab button:hover {
  filter: brightness(90%)
}

.tab button.active {
  background-color: #ccc;
}

.tabcontent {
  display: none;
  padding: 6px 12px;
  border-bottom: 1px solid #000;
  height: 100%;
  width: 100%;
  background: #000;
  color: #fff;
}

#result {
    height: 100%;
  width: 100%;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}

#tabs {
    display: block;
    height: 200px;
    border-top: 1px solid #ccc;
}

@media screen and (min-width: 600px) {
    #tabs {
        display: flex;
    }
    #result {
        width: 100%;
    }
    .tabcontent {
        width: 100%;
    }
}
<div class="tab">
  <button class="tablinks" onclick="openLanguage(event, 'HTML')">HTML</button>
  <button class="tablinks" onclick="openLanguage(event, 'CSS')">CSS</button>
  <button class="tablinks" onclick="openLanguage(event, 'JavaScript')">JavaScript</button>
</div>

<div id="tabs">
<div id="HTML" class="tabcontent">
  <p>Some HTML Code</p>
</div>

<div id="CSS" class="tabcontent">
  <p>Some CSS Code</p> 
</div>

<div id="JavaScript" class="tabcontent">
  <p>Some JS Code</p>
</div>

<div id="result">

</div>

如果单击这些按钮之一,将在输出旁边打开一个选项卡。

我使用了 W3Schools 的这个例子并添加了一个代码部分。它仍然不完美,但你可以看到它是什么。

当您单击活动按钮时,如何才能关闭这些选项卡?

(完全没有jQuery)

克华尔

在这里,我首先currentTab使用 querySelector查找当前选项卡 ( )。隐藏所有选项卡和所有内容后,我测试 currentTab 是否与新选择的选项卡不同,如果是这样,我将使其可见。

顺便提一句。querySelectoAllforEach使得它更容易阅读的代码。

function openLanguage(evt, codeLanguage) {
  var i, tabcontent, tablinks;
  let currentTab = document.querySelector('.tablinks.active');

  tabcontent = document.querySelectorAll("#tabs .tabcontent");
  tabcontent.forEach(content => content.style.display = "none");

  tablinks = document.querySelectorAll(".tab .tablinks");
  tablinks.forEach(tab => tab.classList.remove("active"));

  if (currentTab != evt.currentTarget) {
    document.getElementById(codeLanguage).style.display = "block";
    evt.currentTarget.classList.add("active");
  }
}
body {
  font-family: Arial;
}

.tab {
  overflow: hidden;
  border: 1px solid #ccc;
  border-bottom: none;
  background-color: #f1f1f1;
}

.tab button {
  background-color: inherit;
  float: left;
  border: none;
  outline: none;
  cursor: pointer;
  padding: 14px 16px;
  transition: 0.1s;
  font-size: 17px;
}

.tab button:hover {
  filter: brightness(90%)
}

.tab button.active {
  background-color: #ccc;
}

.tabcontent {
  display: none;
  padding: 6px 12px;
  border-bottom: 1px solid #000;
  height: 100%;
  width: 100%;
  background: #000;
  color: #fff;
}

#result {
  height: 100%;
  width: 100%;
  padding: 6px 12px;
  border: 1px solid #ccc;
  border-top: none;
}

#tabs {
  display: block;
  height: 200px;
  border-top: 1px solid #ccc;
}

@media screen and (min-width: 600px) {
  #tabs {
    display: flex;
  }
  #result {
    width: 100%;
  }
  .tabcontent {
    width: 100%;
  }
}
<div class="tab">
  <button class="tablinks" data-lang="html" onclick="openLanguage(event, 'HTML')">HTML</button>
  <button class="tablinks" data-lang="css" onclick="openLanguage(event, 'CSS')">CSS</button>
  <button class="tablinks" data-lang="js" onclick="openLanguage(event, 'JavaScript')">JavaScript</button>
</div>

<div id="tabs">
  <div id="HTML" class="tabcontent">
    <p>Some HTML Code</p>
  </div>

  <div id="CSS" class="tabcontent">
    <p>Some CSS Code</p>
  </div>

  <div id="JavaScript" class="tabcontent">
    <p>Some JS Code</p>
  </div>

  <div id="result">
  </div>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

jQuery,再次单击时关闭选项卡菜单

我可以从后面的代码中更改按钮单击时的Jquery-ui活动选项卡吗?

jQuery选项卡不活动时如何中断功能?

jQuery tabselect在单击选项卡时不起作用

Django + Jquery +选项卡,单击时不显示选项卡

jQuery 切换:当 1 处于活动状态时关闭所有其他选项卡

按钮未显示从 jquery 选项卡加载时选择

使用jQuery更改活动选项卡

jQuery [选项卡]打开和关闭

jQuery UI选项卡:如果只有一个选项卡,则没有关闭按钮?

单击以打开“ jQuery UI”选项卡

jquery“点击”选项卡内的按钮

jQuery 当单击 Div 时转到新选项卡中的 url,除非单击子项

jQuery在“单击”选项卡上添加类,并在未单击时删除

使用JQUERY在按钮单击上打开新选项卡

Jquery:通过单击单个按钮切换到选项卡?

为什么单击按钮后`JQuery`选项卡会丢失样式

使用jQuery生成时,多个Bootstrap选项卡保持活动状态

Jquery 选项卡 - 在活动时添加一个类

jQuery选项卡一样的效果-单击时显示隐藏div

使用jQuery单击每个选项卡时如何更改链接URL

使 Jquery 项目过滤器选项卡在单击时处于活动状态

即使用户单击“中间按钮”或右键单击->在新选项卡中打开,如何触发jquery的“ click”事件

从选项卡和按钮触发时,jQuery具有相同调用的问题

jQuery将类添加到活动选项卡

jQuery-MixItUp更改默认的“活动”选项卡内容

使用jQuery更改活动的Bootstrap 3选项卡

Bootstrap 3 jQuery事件,用于活动选项卡更改

如何禁用JQuery选项卡上的单击事件?