如何在javascript中创建标签? [英] How to create tabs in javascript?

查看:36
本文介绍了如何在javascript中创建标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试用 JavaScript 创建标签.当点击一个新标签时,活动类应该被应用到它,并且它相关的面板内容应该被显示.但是,当单击另一个选项卡时不会应用 活动类面板 也没有改变,它们只是堆叠在一起.

const tabs = document.querySelector(".tabs");const active = document.querySelector(".active");const panel = document.querySelector(".panel");功能 onTabClick(事件){//停用现有的活动选项卡和面板active.classList.remove('.active');for (let i = 0; i < panel.length; i++) {panel[i].style.display = "none";}//激活新选项卡和面板event.target.classList.add('.active');让 classString = event.target.innerHTML;控制台日志(类字符串);document.getElementsByClassName(classString)[0].style.display = "block";}tabs.addEventListener('click', onTabClick, false);

.tabs {显示:弹性;justify-content: 空间环绕;边距:20px 2px 40px 2px;高度:40px;box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.2);}.tabs>* {宽度:100%;颜色:深灰色;高度:100%;光标:指针;显示:弹性;对齐内容:居中;对齐项目:居中;}.tabs>*:hover:not(.active) {背景颜色:rgb(220, 220, 220);}.tabs>.active {白颜色;背景色:#4CAF50;}.控制板 {显示:无;}

<html lang="en" dir="ltr"><头><meta charset="utf-8"><title></title><身体><div class="tabs"><div class="tab active">列表</div><div class="tab">网格</div><div class="tab">something</div>

<div class="Lists panel" style='display:block'>面板 1 文本

<div class="网格面板">面板 2 文本

<div class="东西面板">面板 3 文本

</html>

解决方案

在你的 JavaScript 中,当获取特定类的所有元素时,document.querySelecto(".className") 只抓取具有给定类的第一个元素.要获取所有元素,您必须使用 document.querySelectorAll(".className").

然后在元素中添加或删除类时,不要将点放在类名之前.所以,而不是 tab[i].classList.remove(".active");,它应该是 tab[i].classList.remove("active");代码>.

此外,我没有明确隐藏面板元素,而是使用了一个 active 类,该类将应用于与面板相关的选项卡.

最后,您不应该使用标签文本作为相应面板的类名.当您在Tab文本中有多个单词时,这将是有问题的.我所做的是使用选项卡的 data 属性来指定面板的相关类,并将所有面板放在 div 元素 .panels 中.现在您可以在选项卡中放置任何您想要的文本,并使用 data-target 属性,放置面板的类名.

const tabs = document.querySelectorAll(".tabs");const tab = document.querySelectorAll(".tab");const panel = document.querySelectorAll(".panel");功能 onTabClick(事件){//停用现有的活动选项卡和面板for (let i = 0; i 

.tabs {显示:弹性;justify-content: 空间环绕;边距:20px 2px 40px 2px;高度:40px;box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.2);}.tabs>* {宽度:100%;颜色:深灰色;高度:100%;光标:指针;显示:弹性;对齐内容:居中;对齐项目:居中;}.tabs>*:hover:not(.active) {背景颜色:rgb(220, 220, 220);}.tabs>.active {白颜色;背景色:#4CAF50;}.控制板 {显示:无;}.panel.active {显示:块;}

<div class="tab active" data-target="Lists">Lists</div><div class="tab" data-target="Grid">Grid</div><div class="tab" data-target="Something">Something</div>

<div id="面板"><div class="列表面板活动">面板 1 文本

<div class="网格面板">面板 2 文本

<div class="Something panel">面板 3 文本

I've been trying to create tabs in JavaScript. When clicking on a new tab the active class should be applied to it as well as its associated panel content should be displayed. However, the active class isn't being applied when another tab is clicked and the panels are not changing either, they are just stacking on top of each other.

const tabs = document.querySelector(".tabs");
const active = document.querySelector(".active");
const panel = document.querySelector(".panel");

function onTabClick(event) {

  // deactivate existing active tabs and panel
  active.classList.remove('.active');

  for (let i = 0; i < panel.length; i++) {
    panel[i].style.display = "none";
  }


  // activate new tabs and panel
  event.target.classList.add('.active');
  let classString = event.target.innerHTML;
  console.log(classString);
  document.getElementsByClassName(classString)[0].style.display = "block";
}

tabs.addEventListener('click', onTabClick, false);

.tabs {
  display: flex;
  justify-content: space-around;
  margin: 20px 2px 40px 2px;
  height: 40px;
  box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.2);
}

.tabs>* {
  width: 100%;
  color: dimgray;
  height: 100%;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
}

.tabs>*:hover:not(.active) {
  background-color: rgb(220, 220, 220);
}

.tabs>.active {
  color: white;
  background-color: #4CAF50;
}

.panel {
  display: none;
}

<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
  <meta charset="utf-8">
  <title></title>
</head>

<body>
  <div class="tabs">
    <div class="tab active">List</div>
    <div class="tab">Grid</div>
    <div class="tab">something</div>
  </div>
  <div class="Lists panel" style='display:block'>
    panel 1 text
  </div>
  <div class="Grid panel">
    panel 2 text
  </div>
  <div class="something panel">
    panel 3 text
  </div>

</body>

</html>

解决方案

In your JavaScript when getting all the elements of a specific class, document.querySelecto(".className") only grabs the first element with the given class. To get all the elements, you'll have to use document.querySelectorAll(".className").

Then when adding or removing a class from an element, you don't put the point before the class name. So instead of tab[i].classList.remove(".active");, it should be tab[i].classList.remove("active");.

Also, instead of explicity hiding the panel elements, I used an active class which will be applied to panel associated tab.

Finally, you should not use the tab text as the class name for the respective panel. This would be problematic when you have multiple words in the tab text. What I did was use the data attribute of the tabs to specify the relative class of the panel and place all the panels in a div element .panels. Now you can put what ever text you want in the tabs and using the data-target attribute, place the class name for the panel.

const tabs = document.querySelectorAll(".tabs");
const tab = document.querySelectorAll(".tab");
const panel = document.querySelectorAll(".panel");

function onTabClick(event) {

  // deactivate existing active tabs and panel

  for (let i = 0; i < tab.length; i++) {
    tab[i].classList.remove("active");
  }

  for (let i = 0; i < panel.length; i++) {
    panel[i].classList.remove("active");
  }


  // activate new tabs and panel
  event.target.classList.add('active');
  let classString = event.target.getAttribute('data-target');
  console.log(classString);
  document.getElementById('panels').getElementsByClassName(classString)[0].classList.add("active");
}

for (let i = 0; i < tab.length; i++) {
  tab[i].addEventListener('click', onTabClick, false);
}

.tabs {
  display: flex;
  justify-content: space-around;
  margin: 20px 2px 40px 2px;
  height: 40px;
  box-shadow: 0 0 1px 1px rgba(0, 0, 0, 0.2);
}

.tabs>* {
  width: 100%;
  color: dimgray;
  height: 100%;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
}

.tabs>*:hover:not(.active) {
  background-color: rgb(220, 220, 220);
}

.tabs>.active {
  color: white;
  background-color: #4CAF50;
}

.panel {
  display: none;
}

.panel.active {
  display: block;
}

<div class="tabs">
  <div class="tab active" data-target="Lists">Lists</div>
  <div class="tab" data-target="Grid">Grid</div>
  <div class="tab" data-target="Something">Something</div>
</div>
<div id="panels">
  <div class="Lists panel active">
    panel 1 text
  </div>
  <div class="Grid panel">
    panel 2 text
  </div>
  <div class="Something panel">
    panel 3 text
  </div>
</div>

这篇关于如何在javascript中创建标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆