【问题标题】:Trying to anchor link to tabs. How to have a link jump to an input anchor and mark it as checked?试图将链接锚定到选项卡。如何让链接跳转到输入锚并将其标记为已选中?
【发布时间】:2020-08-08 02:30:14
【问题描述】:

理想情况下,这将只使用 HTML/CSS 来完成,但我理解这是否不可行。

我在我的页面上使用标签式窗口,例如this。这(目前)通常通过让每个选项卡成为可以“检查”的输入来完成。

<input checked="checked" type="radio" id="tab 1">

现在在我的页面上,顶部还有一个指向锚点的目录。

所以假设我有这样的事情:

<li>
  <input checked="checked" type="radio" id="tab1">
  <label class="tab1-label">Tab 1</label>
  <div id="tab1-content">
    <p>This is the content of tab 1.</p>
  </div>
</li>

<li>
  <input type="radio" id="tab2">
  <label class="tab2-label">Tab 2</label>
  <div id="tab2-content">
    <p>This is the content of tab 2.</p>
  </div>
</li>

<li>
  <input type="radio" id="tab3">
  <label class="tab3-label">Tab 3</label>
  <div id="tab3-content">
    <p>This is the content of tab 3.</p>
  </div>
</li>

有没有办法让我从本页顶部的目录链接并跳转到选项卡并在此过程中激活它(将其标记为选中)?我会想象这样的事情:

<a href="#tab2-anchor">Link to Tab 2</a>

可以跳转到这个:

<a name="tab2-anchor"><input type="radio" id="tab2"></a>

但是它如何将输入切换为选中状态?也许可能需要一些 javascript 来添加/删除它。所以从这里:

<input checked="checked" type="radio" id="tab1">    
<input type="radio" id="tab2">
<input type="radio" id="tab3">

到这里:

<input type="radio" id="tab1">    
<input checked="checked" type="radio" id="tab2">
<input type="radio" id="tab3">

我也愿意使用输入/收音机以外的东西来构建这些选项卡,如果这样可以更轻松地链接到所述选项卡。

这是一个有效的codepen 和下面的功能代码,它具有工作选项卡,但未添加链接和切换到它们。请注意:我试图清理它,但我对任何混乱表示歉意,因为它是拼接在一起的,目前正在开发中。

/* Component Needs */

.pc-tab input,
.pc-tab .tab_content > div {
  display: none;
}

#tab1:checked ~ .tab_container .tab1,
#tab2:checked ~ .tab_container .tab2,
#tab3:checked ~ .tab_container .tab3,
#tab4:checked ~ .tab_container .tab4{
  display: block;
}

#tab1:checked ~ .tabs nav .tab1,
#tab2:checked ~ .tabs nav .tab2,
#tab3:checked ~ .tabs nav .tab3,
#tab4:checked ~ .tabs nav .tab4{
  color: orange;
}

/* Visual Styles */

$activeColor: transparent; /* #ffffff */
$unactiveColor: #eeeeee;
$unactiveHoverColor: red; /* #dddddd */
$offsetHeight: 50px;


*, *:after, *:before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

.tab_content div {
  box-sizing: border-box;
  }

body {
  background: #ecf0f1;
}

h1 {
  text-align: center;
  font-weight: 100;
  font-size: 60px;
  color: #e74c3c;
}

.pc-tab { 
  width: 100%;
  max-width: 700px;
  margin: 0 auto;
  }
  
.pc-tab .tabs ul {
  list-style: none;
  margin: 0;
  padding: 0;
  }
  
.pc-tab .tabs ul li label {  
  font-family: "Raleway";
  float: left;
  padding: 15px 25px;
  border: 1px solid #ddd;
  border-bottom: 0;
  background: /* $unactiveColor */ #eeeeee;
  color: #444;
  }

.pc-tab .tabs ul li label:hover {
  background: /* $unactiveHoverColor */ red;
  }

.pc-tab .tabs ul li label:active {
  background: /* $activeColor */ transparent;
  }
  
.pc-tab .tabs ul li:not(:last-child) label {
  border-right-width: 0; 
  }

.tab_content {
  font-family: "Droid Serif";
  clear: both;
  }
    
.tab_content div {
  padding-top: calc(20px + /* #{$offsetHeight} */ 50px);
  padding-right: 20px;
  padding-left: 20px;
  padding-bottom: 20px;
      
  width: 100%;
  border: 1px solid #ddd;
  /* background: #fff; */
  line-height: 1.5em;
  letter-spacing: 0.3px;
  color: #444;
  }
  
.tab_content div h2 {
  margin: 0;
  font-family: "Raleway";
  letter-spacing: 1px;
  color: #34495e;
  }

#tab1:checked ~ .tabs nav .tab1 label,
#tab2:checked ~ .tabs nav .tab2 label,
#tab3:checked ~ .tabs nav .tab3 label,
#tab4:checked ~ .tabs nav .tab4 label {
  background: transparent;
  color: #111;
  position: relative;
  }
  
#tab1:checked ~ .tabs nav .tab1 label:after,
#tab2:checked ~ .tabs nav .tab2 label:after,
#tab3:checked ~ .tabs nav .tab3 label:after,
#tab4:checked ~ .tabs nav .tab4 label:after {
  content: '';
  display: block;
  position: absolute;
  height: 2px;
  width: 100%;
  background: $activeColor;
  left: 0;
  bottom: -1px;
  }

label {
  width: calc(100% / 4);
  box-sizing: border-box;
  text-align: center;
  height: /* $offsetHeight */ 50px;
  }

.tab_content .tab1 {
  background-color: pink;
  }

.tab_content .tab2 {
  background-color: lightblue;
  }

.tab_content .tab3 {
  background-color: lightyellow;
  }

.tab_content .tab4 {
  background-color: lightgreen;
  }

.tabs {
  height: 100%;
  display: table;
  width: 100%;
  margin-bottom: -50px; /*-$offsetHeight */
  position: relative;
  }

.tab_container {
  width: 100%;
  height: 0;
  padding-bottom: 50%;
  position: relative;
  z-index: -1;
}

.tab_content {
  position: absolute;
  height: 100%;
  width: 100%;
}

.tab_content div {
  height: 100%;
}
<div style="width: 700px; margin: 0 auto;">
  <ul>
    <li><a href="">Link to First Tab</a></li>
    <li><a href="">Link to Second Tab</a></li>
    <li><a href="">Link to Third Tab</a></li>
    <li><a href="">Link to Fourth Tab</a></li>
  </uL> 
</div>

<div style="height:300px;"></div>


<!-- 
ORIGINAL BY RENA.TO 
https://codepen.io/renatorib/pen/rlpfj
-->

<div class="pc-tab">
<input checked="checked" id="tab1" type="radio" name="pct" />
<input id="tab2" type="radio" name="pct" />
<input id="tab3" type="radio" name="pct" />
<input id="tab4" type="radio" name="pct" />  
  <div class="tabs">
  <nav>
    <ul>
      <li class="tab1">
        <label for="tab1">First Tab</label>
      </li>
      <li class="tab2">
        <label for="tab2">Second Tab</label>
      </li>
      <li class="tab3">
        <label for="tab3">Third Tab</label>
      </li>
      <li class="tab4">
        <label for="tab4">Fourth Tab</label>
      </li>
    </ul>
  </nav>
  </div> 
  
  <div class="tab_container">
  <div class="tab_content">
    
    <div class="tab1">
      <p class="title>">First</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus itaque quidem minus nostrum, voluptatem accusamus aspernatur quia harum ratione, officia laudantium inventore autem doloribus atque labore numquam non. Hic, animi.</p>
    </div>
    
    <div class="tab2">
      <p class="title>">Second</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laborum nesciunt ipsum dolore error repellendus officiis aliquid a, vitae reprehenderit, accusantium vero, ad. Obcaecati numquam sapiente cupiditate. Praesentium eaque, quae error!</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, maiores.</p>
    </div>
    
    <div class="tab3">
      <p class="title>">Third</p>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
    </div>
    
    <div class="tab4">
      <h2>Fourth</h2>
      <p> afdadfadfadfad Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio, nobis culpa rem, vitae earum aliquid.</p>
    </div>
    
  </div>
  </div>
  
</div>
  


<div style="height:1000px;"></div>

提前感谢您的帮助。

【问题讨论】:

    标签: javascript html css tabs anchor


    【解决方案1】:

    您可以使用更少的 javascript 来实现这一点。在您的链接中添加label。并在您的标签中添加另一个 id。例如,我添加了id="tab1_content"。然后你应该在点击标签时触发你的链接上的点击事件。

    document.querySelectorAll("label").forEach(p => {
      p.addEventListener("click", () => {
        event.target.parentElement.click();
      });
    });
    /* Component Needs */
    
    a label {
      cursor: pointer;
    }
    
    .pc-tab input,
    .pc-tab .tab_content>div {
      display: none;
    }
    
    #tab1:checked~.tab_container .tab1,
    #tab2:checked~.tab_container .tab2,
    #tab3:checked~.tab_container .tab3,
    #tab4:checked~.tab_container .tab4 {
      display: block;
    }
    
    #tab1:checked~.tabs nav .tab1,
    #tab2:checked~.tabs nav .tab2,
    #tab3:checked~.tabs nav .tab3,
    #tab4:checked~.tabs nav .tab4 {
      color: orange;
    }
    
    
    /* Visual Styles */
    
    $activeColor: transparent;
    
    /* #ffffff */
    
    $unactiveColor: #eeeeee;
    $unactiveHoverColor: red;
    
    /* #dddddd */
    
    $offsetHeight: 50px;
    *,
    *:after,
    *:before {
      -webkit-box-sizing: border-box;
      -moz-box-sizing: border-box;
      box-sizing: border-box;
    }
    
    .tab_content div {
      box-sizing: border-box;
    }
    
    body {
      background: #ecf0f1;
    }
    
    h1 {
      text-align: center;
      font-weight: 100;
      font-size: 60px;
      color: #e74c3c;
    }
    
    .pc-tab {
      width: 100%;
      max-width: 700px;
      margin: 0 auto;
    }
    
    .pc-tab .tabs ul {
      list-style: none;
      margin: 0;
      padding: 0;
    }
    
    .pc-tab .tabs ul li label {
      font-family: "Raleway";
      float: left;
      padding: 15px 25px;
      border: 1px solid #ddd;
      border-bottom: 0;
      background: /* $unactiveColor */
      #eeeeee;
      color: #444;
    }
    
    .pc-tab .tabs ul li label:hover {
      background: /* $unactiveHoverColor */
      red;
    }
    
    .pc-tab .tabs ul li label:active {
      background: /* $activeColor */
      transparent;
    }
    
    .pc-tab .tabs ul li:not(:last-child) label {
      border-right-width: 0;
    }
    
    .tab_content {
      font-family: "Droid Serif";
      clear: both;
    }
    
    .tab_content div {
      padding-top: calc(20px +/* #{$offsetHeight} */
      50px);
      padding-right: 20px;
      padding-left: 20px;
      padding-bottom: 20px;
      width: 100%;
      border: 1px solid #ddd;
      /* background: #fff; */
      line-height: 1.5em;
      letter-spacing: 0.3px;
      color: #444;
    }
    
    .tab_content div h2 {
      margin: 0;
      font-family: "Raleway";
      letter-spacing: 1px;
      color: #34495e;
    }
    
    #tab1:checked~.tabs nav .tab1 label,
    #tab2:checked~.tabs nav .tab2 label,
    #tab3:checked~.tabs nav .tab3 label,
    #tab4:checked~.tabs nav .tab4 label {
      background: transparent;
      color: #111;
      position: relative;
    }
    
    #tab1:checked~.tabs nav .tab1 label:after,
    #tab2:checked~.tabs nav .tab2 label:after,
    #tab3:checked~.tabs nav .tab3 label:after,
    #tab4:checked~.tabs nav .tab4 label:after {
      content: '';
      display: block;
      position: absolute;
      height: 2px;
      width: 100%;
      background: $activeColor;
      left: 0;
      bottom: -1px;
    }
    
    label {
      width: calc(100% / 4);
      box-sizing: border-box;
      text-align: center;
      height: /* $offsetHeight */
      50px;
    }
    
    .tab_content .tab1 {
      background-color: pink;
    }
    
    .tab_content .tab2 {
      background-color: lightblue;
    }
    
    .tab_content .tab3 {
      background-color: lightyellow;
    }
    
    .tab_content .tab4 {
      background-color: lightgreen;
    }
    
    .tabs {
      height: 100%;
      display: table;
      width: 100%;
      margin-bottom: -50px;
      /*-$offsetHeight */
      position: relative;
    }
    
    .tab_container {
      width: 100%;
      height: 0;
      padding-bottom: 50%;
      position: relative;
      z-index: -1;
    }
    
    .tab_content {
      position: absolute;
      height: 100%;
      width: 100%;
    }
    
    .tab_content div {
      height: 100%;
    }
    <div style="width: 700px; margin: 0 auto;">
      <ul>
        <li><a href="#tab1_content"><label for="tab1">Link to First Tab</label></a></li>
        <li><a href="#tab2_content"><label for="tab2">Link to Second Tab</label></a></li>
        <li><a href="#tab3_content"><label for="tab3">Link to Third Tab</label></a></li>
        <li><a href="#tab4_content"><label for="tab4">Link to Fourth Tab</label></a></li>
      </ul>
    </div>
    
    <div style="height:300px;"></div>
    
    
    <!-- 
    ORIGINAL BY RENA.TO 
    https://codepen.io/renatorib/pen/rlpfj
    -->
    
    <div class="pc-tab">
      <input checked="checked" id="tab1" type="radio" name="pct" />
      <input id="tab2" type="radio" name="pct" />
      <input id="tab3" type="radio" name="pct" />
      <input id="tab4" type="radio" name="pct" />
      <div class="tabs">
        <nav>
          <ul>
            <li class="tab1">
              <label for="tab1" id="tab1_content">First Tab</label>
            </li>
            <li class="tab2">
              <label for="tab2" id="tab2_content">Second Tab</label>
            </li>
            <li class="tab3">
              <label for="tab3" id="tab3_content">Third Tab</label>
            </li>
            <li class="tab4">
              <label for="tab4" id="tab4_content">Fourth Tab</label>
            </li>
          </ul>
        </nav>
      </div>
    
      <div class="tab_container">
        <div class="tab_content">
    
          <div class="tab1">
            <p class="title>">First</p>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus itaque quidem minus nostrum, voluptatem accusamus aspernatur quia harum ratione, officia laudantium inventore autem doloribus atque labore numquam non. Hic, animi.</p>
          </div>
    
          <div class="tab2">
            <p class="title>">Second</p>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laborum nesciunt ipsum dolore error repellendus officiis aliquid a, vitae reprehenderit, accusantium vero, ad. Obcaecati numquam sapiente cupiditate. Praesentium eaque, quae error!</p>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, maiores.</p>
          </div>
    
          <div class="tab3" id="tab3_content">
            <p class="title>">Third</p>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
          </div>
    
          <div class="tab4" id="tab4_content">
            <h2>Fourth</h2>
            <p> afdadfadfadfad Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio, nobis culpa rem, vitae earum aliquid.</p>
          </div>
    
        </div>
      </div>
    
    </div>
    
    
    
    <div style="height:1000px;"></div>

    【讨论】:

    • 感谢您提供了一个以我的代码为基础的简洁实用的示例。我已授予您赏金,但不能将其标记为正确答案,因为我能够纯粹使用 CSS 来实现这一点。我的答案将很快发布。
    【解决方案2】:

    我已将编辑后的 ​​HTML 代码附加到它。我所做的只是为每个锚标记添加了各自选项卡的 href 属性。所以“链接到第一个选项卡”将有一个 href =“#tab1”,“链接到第二个选项卡”将有一个 href =“#tab2”等等 检查此链接以获取结果:https://codepen.io/keidakira/pen/MWywNVx

    <div style="width: 700px; margin: 0 auto;">
      <ul>
        <li><a href="#tab1">Link to First Tab</a></li>
        <li><a href="#tab2">Link to Second Tab</a></li>
        <li><a href="#tab3">Link to Third Tab</a></li>
        <li><a href="#tab4">Link to Fourth Tab</a></li>
      </uL> 
    </div>
    
    <div style="height:300px;"></div>
    
    
    <!-- 
    ORIGINAL BY RENA.TO 
    https://codepen.io/renatorib/pen/rlpfj
    -->
    
    <div class="pc-tab">
    <input checked="checked" id="tab1" type="radio" name="pct" />
    <input id="tab2" type="radio" name="pct" />
    <input id="tab3" type="radio" name="pct" />
    <input id="tab4" type="radio" name="pct" />  
      <div class="tabs">
      <nav>
        <ul>
          <li class="tab1">
            <label for="tab1">First Tab</label>
          </li>
          <li class="tab2">
            <label for="tab2">Second Tab</label>
          </li>
          <li class="tab3">
            <label for="tab3">Third Tab</label>
          </li>
          <li class="tab4">
            <label for="tab4">Fourth Tab</label>
          </li>
        </ul>
      </nav>
      </div> 
      
      <div class="tab_container">
      <div class="tab_content">
        
        <div class="tab1">
          <p class="title>">First</p>
          <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus itaque quidem minus nostrum, voluptatem accusamus aspernatur quia harum ratione, officia laudantium inventore autem doloribus atque labore numquam non. Hic, animi.</p>
        </div>
        
        <div class="tab2">
          <p class="title>">Second</p>
          <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laborum nesciunt ipsum dolore error repellendus officiis aliquid a, vitae reprehenderit, accusantium vero, ad. Obcaecati numquam sapiente cupiditate. Praesentium eaque, quae error!</p>
          <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Perferendis, maiores.</p>
        </div>
        
        <div class="tab3">
          <p class="title>">Third</p>
          <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
        </div>
        
        <div class="tab4">
          <h2>Fourth</h2>
          <p> afdadfadfadfad Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio, nobis culpa rem, vitae earum aliquid.</p>
        </div>
        
      </div>
      </div>
      
    </div>
      
    <div style="height:1000px;"></div>
    

    您还需要将 JavaScript 代码添加到您的项目中,以便在单击时滚动到选项卡并更改选项卡。所以我添加了以下Javascript

    <script>
    var tabs = document.getElementsByClassName('tabs')[0];
    for(let i = 0; i < 4; i++) {
      document.getElementsByTagName('a')[i].addEventListener('click', function() {
        document.getElementsByTagName('input').checked = false;
        var hrefLink = this.getAttribute("href");
        var isChecked = document.getElementById(hrefLink.split("#")[1]).checked;
        document.getElementById(hrefLink.split("#")[1]).checked = !isChecked;
        tabs.scrollIntoView();
      });
    }
    </script>
    

    JS (JavaScript) 代码首先将“tabs”元素放入一个变量中,以便稍后使用它进行滚动。由于您有四个选项卡,因此循环执行了四次。在每个循环中,我向锚标记添加了一个函数,它表示当单击锚标记时,检查它的“href”属性以查看它必须打开哪个选项卡。因此,“首先取消选中所有选项卡,然后检查我按下的唯一选项卡”。这就是代码所说的。稍后,使用element.scrollIntoView() 方法转到我存储在变量中的“选项卡”元素。

    希望这能回答你的问题。

    【讨论】:

    • 这很好,但是如果您单击选项卡,则单击选项卡上方的链接将不再显示
    • 链接列表和选项卡之间存在间隙。你不想要那个?
    【解决方案3】:

    使用:target 伪选择器。

    See an example on w3 schools website

    【讨论】:

      【解决方案4】:

      两天后,我使用:checked:target 的组合仅使用HTML/CSS 实现了这一目标。

      这些选项卡通常在所有块(选项卡的实际内容)上使用display: none; 完成,然后使用:checked 切换的任何一个都变为display: block;

      在我发布这个问题之后,我进行了更多挖掘,发现我可以使用:target。但是,当未显示选项卡2(@ 987654327)时,在标签2的ID上使用@ 987654328(#tab2)不起作用,因为页面上不存在(display: none;)。两种解决方法是:

      a) 当:checked:target 时,使用transparency: 0; 切换到transparency: 100;

      b) 使用z-index,当:checked:target 时切换到更高级别。

      我选择使用解决方法 b),因为它会给我更大的控制权。 简而言之,我“未选中”选项卡 1(没有将 checked 分配给 input)。相反,通过给它一个z-index: 99;,同时给其余的标签一个z-index: 98;,让它首先显示(就像选中一样)。接下来,我给目标能力一个z-index: 100;。因此,从页面顶部的链接单击将跳转到相应的选项卡并将其显示在其他选项卡的上方(这将具有较低的 z-index: 98;z-index: 99;)。现在用户在选项卡的页面下方,他们可以单击这些选项卡(因此将它们标记为:checked),这将通过让:checked 给出z-index: 101;(高于任何@987654348)再次覆盖它@ 带有z-index: 100; 的标签(请注意,页面上的网址将在末尾显示目标#tabnumber,因此需要覆盖它,因为它不会消失)。

      唯一的缺点是,如果他们直接点击了一个标签(:checked),向上滚动到标签的链接,点击一个标签的链接(:target),他们会跳到标签,但它不会显示正确的选项卡,因为 :checked 的 z-index 级别高于 :target。不是世界末日,因为这种情况很少发生,他们只需单击一个标签 (:checked) 即可使其工作。

      下面的示例代码(对于混乱的情况表示歉意,因为它已从部署中剥离并使用多个来源拼接在一起)。在下面的示例中,我唯一无法使用的是标签上 :hover 的覆盖,但它适用于我的部署。我会回到这里并稍后修复它。

      /* Component Needs */
      
      .pc-tab > input {
          /* opacity: 0; */
          visibility: hidden;
          position: absolute;
          
          }
          
      .pc-tab .tab_content > div {
        /* display: none; */
          display: block;
          position: absolute;
          }
      
      #tab1_content {
          z-index: 99;
          }
      
      #tab2_content {
          z-index: 98;
          }
          
      #tab3_content {
          z-index: 98;
          }
          
      #tab4_content {
          z-index: 98;
          }
      
          
      #tab1:target ~ .tab_container .tab1,
      #tab2:target ~ .tab_container .tab2,
      #tab3:target ~ .tab_container .tab3,
      #tab4:target ~ .tab_container .tab4{
        /* display: block; */
          z-index: 100; /* 101 */
          }  
          
      #tab1:checked ~ .tab_container .tab1,
      #tab2:checked ~ .tab_container .tab2,
      #tab3:checked ~ .tab_container .tab3,
      #tab4:checked ~ .tab_container .tab4{
        /* display: block; */
          z-index: 101;
          }
          
      
      /* tab visual function */
          
      .tabs nav .tab1 label {
          background: transparent;
          }    
      
      #tab1:target ~ .tabs nav .tab1 label,
      #tab2:target ~ .tabs nav .tab2 label,
      #tab3:target ~ .tabs nav .tab3 label,
      #tab4:target ~ .tabs nav .tab4 label {   
          background: transparent;
          }
          
      #tab1:target ~ .tabs nav li:not(.tab1) label,
      #tab2:target ~ .tabs nav li:not(.tab2) label,
      #tab3:target ~ .tabs nav li:not(.tab3) label,
      #tab4:target ~ .tabs nav li:not(.tab4) label {   
          background: #eeeeee;
          }
      
      #tab1:checked ~ .tabs nav li:not(.tab1) label,
      #tab2:checked ~ .tabs nav li:not(.tab2) label,
      #tab3:checked ~ .tabs nav li:not(.tab3) label,
      #tab4:checked ~ .tabs nav li:not(.tab4) label {   
          background: #eeeeee !important;
          }
      
          
          
      
      #tab1:checked ~ .tabs nav .tab1 label,
      #tab2:checked ~ .tabs nav .tab2 label,
      #tab3:checked ~ .tabs nav .tab3 label,
      #tab4:checked ~ .tabs nav .tab4 label {
          background: transparent !important;
          color: #111;
          position: relative;
          }
          
      #tab1:checked ~ .tabs nav .tab1 label:after,
      #tab2:checked ~ .tabs nav .tab2 label:after,
      #tab3:checked ~ .tabs nav .tab3 label:after,
      #tab4:checked ~ .tabs nav .tab4 label:after {
          background: transparent !important;
          }
             
      label {
          cursor: pointer;
          }    
          
      
      
      /* Visual Styles */
      
      $activeColor: transparent; /* #ffffff */
      $unactiveColor: #eeeeee;
      $unactiveHoverColor: red; /* #dddddd */
      $offsetHeight: 50px;
      
      
      /* *, *:after, *:before {
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
      } */
      
      .pc-tab *,
      .pc-tab *:after, 
      .pc-tab *:before {
          -webkit-box-sizing: border-box;
          -moz-box-sizing: border-box;
          box-sizing: border-box;
      }
      
      
      .pc-tab { 
          width: 100%;
          /* max-width: 700px; */
          margin: 0 auto;
          }
      
      .tabs ul {
          list-style: none;
          margin: 0;
          padding: 0;
          }
      
          
      .tabs ul li label {
          /* font-family: "Raleway"; */
          font-size: 15px;
          line-height: 21px;
          
          float: left;
          padding: 15px 10px;
          border: 1px solid #ddd;
          border-bottom: 0;
          background: #eee; }};
          color: #444;
          }
          
          
          
      
      
          
      #over #tab1 ~ .tabs nav .tab1 label:hover,
      #over #tab2 ~ .tabs nav .tab2 label:hover,
      #over #tab3 ~ .tabs nav .tab3 label:hover,
      #over #tab4 ~ .tabs nav .tab4 label:hover,
      #over .tabs ul li label:hover,
      label:hover {
          background: red !important;
          }
      
      
      .tabs ul li label:active {
          background: transparent;
          }   
      
      .tabs ul li label:not(:last-child) {
          border-right-width: 0;
          }
        
          
      .tab_content {
          clear: both;
          }
          
      .tab_content div {
            padding-bottom: 20px;
            padding-right: 20px;
            padding-left: 20px;
            padding-top: calc(20px + 50px);
            
            width: 100%;
            border: 1px solid #ddd;
            /* background: #fff; */
            line-height: 1.5em;
            letter-spacing: 0.3px;
            color: #444;
              
          box-sizing: border-box;
          }
          
      .tab_content div h2 {
          margin: 0;
          font-family: "Raleway";
          letter-spacing: 1px;
          color: #34495e;
          }
      
      
      
      label {
          width: calc(100% / 4);
          box-sizing: border-box;
          text-align: center;
          height: 50px;
          }
      
      nav {
          }
          
      .tab_content .tab1 {
        background-color: pink;
        }
      
      .tab_content .tab2 {
        background-color: lightblue;
        }
      
      .tab_content .tab3 {
        background-color: lightyellow;
        }
      
      .tab_content .tab4 {
        background-color: lightgreen;
        }    
          
          
      
      .tabs {
          height: 100%;
          display: table;
          width: 100%;
          margin-bottom: -50px;
          position: relative;
          z-index: 2;
          }
          
      .tabs li {
          margin-bottom: 0;
          }
      
      .tab_container {
          width: 100%;
          height: 0;
          padding-bottom: calc((100% / 16) * 9);
          position: relative;
          z-index: 1;
          }
      
      .tab_content {
          position: absolute;
          height: 100%;
          width: 100%;
          }
      
      .tab_content div {
          height: 100%;
          }
         
          
      .title {
          padding: 0;
          }
      <div style="width: 700px; margin: 0 auto;">
        <ul>
          <li><a href="#tab1">Link to First Tab</a></li>
          <li><a href="#tab2">Link to Second Tab</a></li>
          <li><a href="#tab3">Link to Third Tab</a></li>
          <li><a href="#tab4">Link to Fourth Tab</a></li>
        </uL> 
      </div>
      
      <div style="height:300px;"></div>
      
      
      <!-- 
      ORIGINAL BY RENA.TO 
      https://codepen.io/renatorib/pen/rlpfj
      -->
      
              <div class="pc-tab" id="over">
          
      <input id="tab1" type="radio" name="pct" />
      <input id="tab2" type="radio" name="pct" />
      <input id="tab3" type="radio" name="pct" />
      <input id="tab4" type="radio" name="pct" />  
        
      <div class="tabs">
        <nav>
          <ul>
            <li class="tab1">
              <label for="tab1">First Tab</label>
            </li>
            <li class="tab2">
              <label for="tab2">Second Tab</label>
            </li>
            <li class="tab3">
              <label for="tab3">Third Tab</label>
            </li>
            <li class="tab4">
              <label for="tab4">Fourth Tab</label>
            </li>
          </ul>
        </nav>
      </div>   
      
        
      <div class="tab_container">
        <div class="tab_content">
          
          <div class="tab1 tab_block" id="tab1_content">
            <p class="title">First</p>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Repellendus itaque quidem minus nostrum, voluptatem accusamus aspernatur quia harum ratione, officia laudantium inventore autem doloribus atque labore numquam non. Hic, animi.</p>
          </div>
          
          <div class="tab2 tab_block" id="tab2_content">
            <p class="title">Second</p>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Laborum nesciunt ipsum dolore error repellendus officiis aliquid a, vitae reprehenderit, accusantium vero, ad. Obcaecati numquam sapiente cupiditate. Praesentium eaque, quae error!</p>
          </div>
          
          <div class="tab3 tab_block" id="tab3_content">
            <p class="title">Third</p>
            <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
          </div>
          
          <div class="tab4 tab_block" id="tab4_content">
            <p class="title">Fourth</p>
            <p> afdadfadfadfad Lorem ipsum dolor sit amet, consectetur adipisicing elit. Optio, nobis culpa rem, vitae earum aliquid.</p>
          </div>
          
        </div>
      </div>        
      
        
              </div>
      
      
      <div style="height:1000px;"></div>

      【讨论】:

      • 真的很棒
      【解决方案5】:

      您可以使用纯 HTML 来实现这一点。这里是:

      <a href = "#tab1">Jump to Tab 1</a>
      <a href = "#tab2">Jump to Tab 2</a>
      <a href = "#tab3">Jump to Tab 3</a>
      

      希望这有效。

      【讨论】:

        【解决方案6】:

        用纯html+css这个结果是无法达到的,因为a href不保留改变的样式,而input不能有href
        最简单的解决方案是 html checked 和 js href 与 css :checked:target

        为此,您需要input type="radio"。所有inputs 必须具有相同的name 和不同的values 才能正常工作。
        像下面这样的 HTML 代码不起作用

        <label><input type="radio" name="bad" value="0">caption</label>
        

        是绝对有效的,但是对于像input + label 这样的css 选择器,input:checked + label input 不能在里面。为此,您需要更长(丑陋)的写入方法:

        <input id="unique" type="radio" name="good" value="1">
        <label for="unique">caption</label>     
        

        为什么不checkbox
        type="radio"name相同的checked中只有一个type="checkbox"name相同可以同时为checked,更糟糕的是checkbox onclick改变了@987654345的状态@,但每次点击时 js 都会跳转并指向一个新的:target - 所以,这不是你需要的。

        作为一个 sn-p,我给你我的整个测试页面 - 拿走它,玩它 - 我想到了每一个案例,看看它是如何工作的。每一段代码都是正确的。

        最简单的组织者切换内容的关键部分代码:

        &lt;!DOCTYPE html&gt;&lt;head&gt;&lt;style&gt; - 仅用于本地测试的缩短

        p, input{ display: none}                /* p as container of switched text */
        :target{ display: block}                /* address in clicked href="" */
        input + label{ text-align: center; display: inline-block; padding: 10px; width: 150px}
        input:checked + label { font-weight: bold}
        

        &lt;/style&gt;&lt;/head&gt;&lt;body&gt;

        <input type="radio" id="a1" name="radio" value="1" onclick="jump('one')"><label for="a1">one</label>
        <input type="radio" id="a2" name="radio" value="2" onclick="jump('two')"><label for="a2">two</label>
        <input type="radio" id="a3" name="radio" value="3" onclick="jump('three')"><label for="a3">three</label>
        <p id="one"><b>ONE</b> simplest js content switcher </p>
        <p id="two"><b>TWO</b> this is <i>&lt;p id="two"&gt;</i> as a <i>:target</i> of js function <i>jump('two')</i></p>
        <p id="three"><b>THREE</b> ... </p>
        

        ...其他内容...

        <script> function jump(x){ location.href = "#" + x}</script>
        </body></html>
        

        供您参考,在纯 HTML 中有 iframedetails 之类的标签,它们也增加了窗口容量,没有任何 js。

         function jump(x){ location.href = "#" + x}
        *{ box-sizing: border-box}
        p, input{ display: none}
        .show{ display: block}
        :target{ display: block}
        input + label{ text-align: center; display: inline-block; padding: 10px}
        input[type=radio] + label{ width: 150px}
        input[type=checkbox] + label{ width: 10%}
        input:checked + label { font-weight: bold}
        body{ background: #013; color: #fff}
        div{ background: 0; padding: 0; margin: 0 0 10px; }
        p{ padding: 20px; background: 0; color: #abc}
        div p{ margin: 0; border-radius: 0 10px 10px;}
        .show{ margin: 20px auto; border-radius: 20px; width: 90%;}
        :target{ background: #fff; color: #123; }
        input + label{ margin: 2px 0 0; border-radius: 20px 10px 0 0; color: #345; background: linear-gradient( to top, #fff, #ace, #579); border: 1px solid #ace; }
        input:checked + label, i {  background: #fff; color: #56b; border-color: #fff}
        :not([name=radio]):checked + label{ border-radius: 20px 100px;}
        [name="box"]+ label, [name="box"]:checked + label{ border-radius: 10px }
        b{ display: block; padding: 0 50px 20px}
        details{  width: 80%; margin: 10px auto}
        details[open]{ height: 1000px;}
        summary{ text-align: center; margin: 0 auto; display: block}
        details[open] summary{ background: #fff; margin: 50px auto; padding: 10px}
        #first{ min-height: 120px;}
        #last{ text-align: center; margin: 0 auto 50px}
        a, summary{ text-decoration: none; outline: 0; color: #456; }
        <input type="radio" id="a1" name="radio" value="1" onclick="jump('one')"><label for="a1">one</label>
        <input type="radio" id="a2" name="radio" value="2" onclick="jump('two')"><label for="a2">two</label>
        <input type="radio" id="a3" name="radio" value="3" onclick="jump('three')"><label for="a3">three</label>
        <input type="radio" id="a4" name="radio" value="4" onclick="jump('four')"><label for="a4">four</label>
        <input type="radio" id="a5" name="radio" value="5" onclick="jump('five')"><label for="a5">five</label>
        <input type="radio" id="a6" name="radio" value="6" onclick="jump('six')"><label for="a6">six</label>
        <input type="radio" id="a7" name="uno" value="7" onclick="jump('two')"><label for="a7">&lowast; two</label>
        <input type="radio" id="a8" name="due" value="8" onclick="jump('five')"><label for="a8">&lowast; five</label>
        <a href="">&lowast;</a>
        <div id="first">
        <p id="one"><b>ONE</b> simplest js content switcher </p>
        <p id="two"><b>TWO</b> this is <i>&lt;p id="two"&gt;</i> as a <i>:target</i> of js function <i>jump('two')</i></p>
        <p id="three"><b>THREE</b> ... </p>
        </div>
        <details><summary>click me to change content size</summary>Limitations:<ul><li>using js is now (almost) safe - browsers finally support events, recently even Safari, but on older devices - content will be unavailble</li><li>this kind of layout is not inherently responsive - it can be done well, even mobile-first (latest approach) but it's not easy</li><li>all content is loaded on startup (using &lt;iframe&gt; only partially)</li><li>in case of css loading failure the page will be ugly - don't use multiple or huge css files</li></ul>
        </details>
        <p id="four" class="show"><b>FOUR</b> The top row is type="radio" and the bottom row is type="checkbox". Both types 6x of same name and 2 radio strangers - to see how it works.<br>Above right, a small anchor to the initial view</p>
        <p id="five" class="show"><b>FIVE</b> Checkbox onclick changes the state of checked / unchecked, but js just jumps and points to a new :target with each click</p>
        <p id="six" class="show"><b>SIX</b> ... </p>
        <div id="last">
        <input type="checkbox" id="b1" name="box" value="1" onclick="jump('one')"><label for="b1">one</label>
        <input type="checkbox" id="b2" name="box" value="2" onclick="jump('two')"><label for="b2">two</label>
        <input type="checkbox" id="b3" name="box" value="3" onclick="jump('three')"><label for="b3">three</label>
        <input type="checkbox" id="b4" name="box" value="4" onclick="jump('four')"><label for="b4">four</label>
        <input type="checkbox" id="b5" name="box" value="5" onclick="jump('five')"><label for="b5">five</label>
        <input type="checkbox" id="b6" name="box" value="6" onclick="jump('six')"><label for="b6">six</label>
        </div>

        【讨论】:

        • 不幸的是,您的最终 show_code_sn-p 示例有些问题。也许你误解了我试图完成的跳跃?
        • @Davbog 我的代码没问题,它是程序员的教育玩具,而不是快餐
        猜你喜欢
        • 1970-01-01
        • 2019-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-15
        • 2014-08-01
        • 1970-01-01
        相关资源
        最近更新 更多