【问题标题】:switch tab by using buttons [closed]使用按钮切换选项卡[关闭]
【发布时间】:2019-02-18 21:54:41
【问题描述】:

当我的网页用户按下选项卡 2 时,如果他按下下一个,我想向他显示下一个选项卡。即标签 3。我不知道如何将这两个值相互关联。

如果我们能够将选项卡值存储在某处并且按钮可以访问该值,那么在这些之后我们可以通过递增或递减来更改选项卡。

我想将标签与按钮相关联。当用户单击选项卡 3 时,它将打开选项卡 3 并隐藏其他选项卡。当用户单击“下一步”按钮时,应显示选项卡 4。

【问题讨论】:

  • 您考虑过使用任何框架吗? Bootstrap 有一个完整的选项卡解决方案。
  • 这里是我想要的,通过按钮更改标签。
  • 这个问题已经回答过很多次了。请查看此帖子:stackoverflow.com/questions/7171504/…
  • 欢迎来到 Stack Overflow。请指出您有多少个标签。请edit 包含minimal reproducible example 的问题。

标签: javascript jquery html


【解决方案1】:

这是在 HTML 上使用数据属性的基本实现。

想法是单击按钮以检索数据属性值(相应 HTML 选项卡内容的 ID)并使其处于活动状态,同时隐藏所有其他内容。

const buttons = document.querySelectorAll(".tab-container > button");
const content = document.querySelectorAll(".tab-content-container > *");

buttons.forEach(button=>{
  button.addEventListener("click", function(e){
      const id = this.dataset.target;
      const target = document.querySelector(id);
      content.forEach(ele=>{
        if(ele === target) ele.classList.add("active");
        else ele.classList.remove("active");
      });
  });
});
.tab-content-container {
  visibility: hidden;
}

.tab-content-container > .active {
  visibility: visible;
  background-color: blue;
}
<div class="tab-content-container">
  <div id="content-1">
    Content 1
  </div>

  <div id="content-2">
    Content 2
  </div>
</div>
<div class="tab-container">
  <button data-target="#content-1">Tab 1</button>
  <button data-target="#content-2">Tab 2</button>
</div>

【讨论】:

    【解决方案2】:

    我为您创建了一个小型 vanillaJS 代码笔。它没有完全优化(代码冗余),但它工作正常:)

    https://codepen.io/anon/pen/BMqgNp

    document.querySelectorAll(".tabscontroller a").forEach(function(e) {
      e.addEventListener("click", function() {
        // Removing all active
        document.querySelectorAll(".tabscontroller a.active, .tabs > .tab.active").forEach(function(b) {
          b.classList.remove("active");
        });
    
        // Activating tab
        e.classList.add("active");
        var tabid = e.getAttribute("data-tab");
        document.querySelector(".tab[data-tab='"+ tabid +"']").classList.add("active");
      });
    })
    
    // Next button
    document.querySelector("#next").addEventListener("click", function() {
      var tabid = parseInt(document.querySelector(".tabs > .tab.active").getAttribute("data-tab"))+1;
      if(tabid > document.querySelectorAll(".tabscontroller a").length) tabid = 1;
      document.querySelectorAll(".tabscontroller a.active, .tabs > .tab.active").forEach(function(b) {
          b.classList.remove("active");
      });
      document.querySelectorAll(".tabscontroller a[data-tab='"+tabid+"'], .tabs > .tab[data-tab='"+tabid+"']").forEach(function(b) {
          b.classList.add("active");
        });
    });
    
    // Prev button
    document.querySelector("#back").addEventListener("click", function() {
      var tabid = parseInt(document.querySelector(".tabs > .tab.active").getAttribute("data-tab"))-1;
      if(tabid < 1) tabid = document.querySelectorAll(".tabscontroller a").length;
      document.querySelectorAll(".tabscontroller a.active, .tabs > .tab.active").forEach(function(b) {
          b.classList.remove("active");
      });
      document.querySelectorAll(".tabscontroller a[data-tab='"+tabid+"'], .tabs > .tab[data-tab='"+tabid+"']").forEach(function(b) {
          b.classList.add("active");
        });
    });
    
    

    编辑:添加上一个按钮返回

    【讨论】:

    • 完美。我们也可以制作上一个按钮吗?就像我在 javascript 中的新手一样。查询选择器不好,所以请
    • 我在代码笔中添加了后退按钮codepen.io/anon/pen/BMqgNp 我还在响应中添加了解释。
    猜你喜欢
    • 2014-08-07
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 1970-01-01
    • 2021-11-28
    • 1970-01-01
    相关资源
    最近更新 更多