简体   繁体   English

显示当前标签页

[英]display current tab javascript

I have a tabbed section that changes the content when selecting a new tab but I want it to always show the home tab when the pages loads. 我有一个选项卡式部分,可以在选择新选项卡时更改内容,但是我希望它在页面加载时始终显示主选项卡。 Right now, no tabs displays when the page loads. 现在,页面加载时没有选项卡显示。 I want the home tab to show when you first visit the page, Thanks! 我希望您第一次访问该页面时显示主页选项卡,谢谢!

Here is the codepen https://codepen.io/emberwolves/pen/rorEmK 这是codepen https://codepen.io/emberwolves/pen/rorEmK

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<section id="tabbed-wrapper">
 <div id="navSection">
   <a href="#home" class="navLink">Home</a>
   <a href="#work" class="navLink">Work</a>
   <a href="#about" class="navLink">About</a>
   <a href="#contact" class="navLink">Contact</a>
 </div>
 <div id="sections">
   <div id="home" class="tabs">
     <h4>Home Section</h4>
     <p>Welcome. Rather see a CSS only version?</p>
     <p>Check it out <a href='https://codepen.io/EstenGrove/pen/jePMXy' target="_blank">here</a></p>
</div>
<div id="work" class="tabs">
  <h4>Work Section</h4>
  <p>For a pure CSS version check out <a href='https://codepen.io/EstenGrove/pen/jePMXy' target="_blank">here</a></p>
</div>
<div id="about" class="tabs">
  <h4>About Section</h4>
  <p>This was a quick little pen for fun. Don't mind the ugly styling.</p>
</div>
<div id="contact" class="tabs">
  <h4>Contact Section</h4>
  <p>Some random contact details...</p>
  </div>
 </div>
</section>

    //nav links
    const links = document.querySelectorAll('.navLink');
    //Tabbed sections
    const tabs = document.querySelectorAll('.tabs');

    links.forEach(function(link) {
        link.addEventListener('click', function(e) {
          e.preventDefault();
          //remove current class from inactive links
          for(let j = 0; j < links.length; j++) {
            links[j].classList.remove('current');
          }
          //add current class to active link
          e.target.classList.add('current');

            //used to store the target ID in string format
            let target = e.target.textContent.toLowerCase();

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

            //Show active tab
            document.getElementById(target).classList.toggle('active');

        }, false)

    });

Just add initial classes in your html: 只需在您的html中添加初始类:

1) First place is nav menu: 1)首先是导航菜单:

<a href="#home" class="navLink current">Home</a>

2) Second place is home tab: 2)第二个是主页选项卡:

<div id="home" class="tabs active">
  <h4>Home Section</h4>
  <p>Welcome. Rather see a CSS only version?</p>
  <p>Check it out <a href='https://codepen.io/EstenGrove/pen/jePMXy' target="_blank">here</a></p>
</div>

Full example: 完整示例:

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<section id="tabbed-wrapper">
  <div id="navSection">
    <a href="#home" class="navLink current">Home</a>
    <a href="#work" class="navLink">Work</a>
    <a href="#about" class="navLink">About</a>
    <a href="#contact" class="navLink">Contact</a>
  </div>
  <div id="sections">
    <div id="home" class="tabs active">
      <h4>Home Section</h4>
      <p>Welcome. Rather see a CSS only version?</p>
      <p>Check it out <a href='https://codepen.io/EstenGrove/pen/jePMXy' target="_blank">here</a></p>
    </div>
    <div id="work" class="tabs">
      <h4>Work Section</h4>
      <p>For a pure CSS version check out <a href='https://codepen.io/EstenGrove/pen/jePMXy' target="_blank">here</a></p>
    </div>
    <div id="about" class="tabs">
      <h4>About Section</h4>
      <p>This was a quick little pen for fun. Don't mind the ugly styling.</p>
    </div>
    <div id="contact" class="tabs">
      <h4>Contact Section</h4>
      <p>Some random contact details...</p>
    </div>
  </div>
</section>

you need to add click method on it . 您需要在其上添加click方法。

document.getElementById("test2").click(); 

For more info . 有关更多信息 And also you need to add id or class to your html tag 而且您还需要向HTML标记添加ID或类

<a href="#home" id="test2" class="navLink">Home</a>

your codepen edited. 您的Codepen已编辑。 https://codepen.io/anon/pen/aPjebe https://codepen.io/anon/pen/aPjebe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM