【问题标题】:Toggle the status of a checkbox in the checkbox hack with a hyperlink使用超链接在复选框 hack 中切换复选框的状态
【发布时间】:2019-09-30 06:00:10
【问题描述】:

我和我的同事在一个包含常见问题解答页面的网站上工作。每个问题之后,都有一个“回答”(Antwoord)扰流板按钮。单击该按钮会在当前问题下方和下一个问题上方显示答案。再次单击该按钮将隐藏答案。这样可以轻松滚动浏览所有问题的列表并找到您可能感兴趣的问题。

现在我面临的问题是我想从问题 1 的答案中链接到例如问题 5 的答案。

我可以为包含问题 5 的 <div> 提供一个 id= 属性以链接到该问题,但理想情况下,我想通过使超链接“单击”答案按钮来立即显示答案。

事实证明,我需要修改复选框 hack 才能使其正常工作,但我面临的问题是我不能在一段文本上同时拥有超链接和标签。

明确一点:我正在寻找使用 HTML/CSS的解决方案。


我的原始代码只有一个剧透按钮

.spoilerbutton {
  display: block;
  border: none;
  padding: 0px 0px;
  margin: 10px 0px;
  font-size: 150%;
  font-weight: bold;
  color: #000000;
  background-color: transparent;
  outline: 0;
}

.spoiler {
  overflow: hidden;
}

.spoiler>div {
  -webkit-transition: all 0s ease;
  -moz-transition: margin 0s ease;
  -o-transition: all 0s ease;
  transition: margin 0s ease;
}

.spoilerbutton[value="Antwoord"]+.spoiler>div {
  margin-top: -500%;
}

.spoilerbutton[value="Hide Antwoord"]+.spoiler {
  padding: 5px;
}
<strong>1. Question 1?</strong> <input class="spoilerbutton" onclick="this.value=this.value=='Antwoord'?'Verberg':'Antwoord';" type="button" value="Antwoord" />
<div class="spoiler">
  <div>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>

尝试从以下位置实施解决方案: Can I have an onclick effect in CSS?

我得到以下不起作用:

#btnControll1 {
    display: none;
}

.spoilerbutton {
  display: block;
  border: none;
  padding: 0px 0px;
  margin: 10px 0px;
  font-size: 150%;
  font-weight: bold;
  color: #000000;
  background-color: transparent;
  outline: 0;
}

.spoiler {
  overflow: hidden;
}

.spoiler>div {
  -webkit-transition: all 0s ease;
  -moz-transition: margin 0s ease;
  -o-transition: all 0s ease;
  transition: margin 0s ease;
}

#btnControl1:checked + label {
margin-top; -500%
}
<strong>1. Question 1?</strong> <input type"chekbox" id="btnControl1" />
<label class="spoilerbutton" for="btnControl1">Antwoord</label>
<div class="spoiler">
  <div>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>

使用 &lt;label&gt;&lt;a href&gt; 的组合(也不起作用)

.spoilerbutton {
display: none;
}
.spoiler {
overflow: hidden;
height:2em;
}
.spoiler>div {
-webkit-transition: all 0s ease;
-moz-transition: margin 0s ease;
-o-transition: all 0s ease;
transition: margin 0s ease;
}
.spoilerbutton+.spoiler>div {
margin-top: -500%;
}
.spoilerbutton+.spoiler>label:before {
content: 'Antwoord';
font-size: 150%;
font-weight: bold;
color: #000000;
}
.spoilerbutton:checked+.spoiler>label:before {
content: 'Verberg';
}
.spoilerbutton:checked+.spoiler {
height:auto;
}
.spoilerbutton:checked+.spoiler>div {
margin-top: 0;
}
<strong>1. Question 1?</strong> <input type="checkbox" id="question1" value="checked" class="spoilerbutton">
<div class="spoiler">
<label for="question1"></label>
<div>
<ul>
<li><a href="#question5"><label for="#answer5">Open 5. link first</label></a></li>
<li><label for="question5"><a href="#answer5">Open 5. label first</a></label></li>
<li><a href="#answer5">Open 5. link only</a></li>
</ul>
</div>
</div>
<strong>2. Question 2?</strong> <input type="checkbox" id="question2" value="checked" class="spoilerbutton">
<div class="spoiler">
<label for="question2"></label>
<div>
<ul>
<li>possible answer 1.</li>
<li>possible answer 2.</li>
<li>possible answer 3.</li>
</ul>
</div>
</div>
<strong>2. Question 3?</strong> <input type="checkbox" id="question3" value="checked" class="spoilerbutton">
<div class="spoiler">
<label for="question3"></label>
<div>
<ul>
<li>possible answer 1.</li>
<li>possible answer 2.</li>
<li>possible answer 3.</li>
</ul>
</div>
</div>
<strong>2. Question 4?</strong> <input type="checkbox" id="question4" value="checked" class="spoilerbutton">
<div class="spoiler">
<label for="question4"></label>
<div>
<ul>
<li>possible answer 1.</li>
<li>possible answer 2.</li>
<li>possible answer 3.</li>
</ul>
</div>
</div>
<br>
<br>
<br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br>
<strong>2. Question 5?</strong> <input type="checkbox" id="question5" value="checked" class="spoilerbutton">
<div class="spoiler" id="answer5">
<label for="question5"></label>
<div>
<ul>
<li>possible answer 1.</li>
<li>possible answer 2.</li>
<li>possible answer 3.</li>
</ul>
</div>
</div>

也相关 How to toggle a checkbox when a link is clicked?

那里有一些很棒的建议,但我无法使 answer from Tushar 适应我的情况。

【问题讨论】:

  • 您已经在 onclick 属性中使用 JavaScript。属性中的那一行行吗?另外,您想点击一个超链接并打开另一个答案?
  • @Aaron3219,好吧,我猜是这样,看到这是我们目前使用的并且它有效。用你知之甚少的语言做某事,赞!
  • 我的第二个问题呢?我没听错吗?
  • 如果我点击 go to answer 5(在我 Q 的最后一个 sn-p 中),我希望屏幕的焦点在问题 5 上,并且答案立即可见。
  • @Luuklag 我更新了我的答案,您可以找到CSSjQuery 这两个选项的工作示例。谢谢

标签: html css button hyperlink show-hide


【解决方案1】:

选项 1 - 使用 JavaScript

重定向仅在元素可见时有效,而不是设置为display: none。在上面的代码 sn-p 中,复选框和答案列表都设置为 display: none 作为它们的初始状态。

第 1 步:id 添加到每个&lt;strong id="question"&gt; 元素。

<strong id="question1">1. Question 1?</strong> 

第 2 步: 我们不能使用 CSS 来选中/取消选中复选框,我们必须使用 JavaScript/jQuery 来实现此功能。因此,当用户单击链接时,我将基础知识添加到复选框 checked 脚本中。

<li><a href="#question5" onClick="var str = this.attributes['href'].value + 'Checkbox'; document.getElementById(str.replace('#', '')).checked = true;">Open 5. link first</a></li>

.spoilerbutton {
display: none;
}
.spoiler {
overflow: hidden;
height:2em;
}
.spoiler>div {
-webkit-transition: all 0s ease;
-moz-transition: margin 0s ease;
-o-transition: all 0s ease;
transition: margin 0s ease;
}
.spoilerbutton+.spoiler>div {
margin-top: -500%;
}
.spoilerbutton+.spoiler label:before {
content: 'Antwoord';
font-size: 150%;
font-weight: bold;
color: #000000;
}
.spoilerbutton:checked+.spoiler label:before {
content: 'Verberg';
}
.spoilerbutton:checked+.spoiler {
height:auto;
}
.spoilerbutton:checked+.spoiler>div {
margin-top: 0;
}
<strong id="question1">1. Question 1?</strong> 
<input type="checkbox" id="question1Checkbox" value="checked" class="spoilerbutton">
  <div class="spoiler">
    <label for="question1Checkbox"></label>
    <div>
      <ul>
        <li><a href="#question5" onClick="var str = this.attributes['href'].value + 'Checkbox'; document.getElementById(str.replace('#', '')).checked = true;">Open 5. link first</a></li>
      </ul>
    </div>
</div>
<strong id="question2">2. Question 2?</strong> 
<input type="checkbox" id="question2Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler">
  <label for="question2Checkbox"></label>
  <div>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>
<strong id="question3">2. Question 3?</strong> 
<input type="checkbox" id="question3Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler">
  <label for="question3Checkbox"></label>
  <div>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>
<strong id="question4">2. Question 4?</strong> 
<input type="checkbox" id="question4Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler">
  <label for="question4Checkbox"></label>
  <div>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>
<br>
<br>
<br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br>
<strong id="question5">2. Question 5?</strong> 
<input type="checkbox" id="question5Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler" id="answer5">
  <label for="question5Checkbox"></label>
  <div>
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>

选项 2 - 使用 CSS

通过使用 CSS :target 选择器和一点点结构更新,我们可以使用 CSS 实现功能。

.spoilerbutton {
display: none;
}
.spoiler {
overflow: hidden;
height:2em;
}
.spoiler>div {
-webkit-transition: all 0s ease;
-moz-transition: margin 0s ease;
-o-transition: all 0s ease;
transition: margin 0s ease;
}
.spoilerbutton+.spoiler .listOfAnswer {
margin-top: -500%;
}
.spoilerbutton+.spoiler .labelWrap {
  display: inline-block;
  position: relative;
}
.spoilerbutton+.spoiler .labelWrap label:before {
content: 'Antwoord';
font-size: 150%;
font-weight: bold;
color: #000000;
}
.spoilerbutton:checked+.spoiler .labelWrap label:before {
content: 'Verberg';
}
.spoilerbutton:checked+.spoiler {
height:auto;
}
.spoilerbutton:checked+.spoiler .listOfAnswer {
margin-top: 0;
}

:target + .spoilerbutton + .spoiler {
height:auto;
}
:target + .spoilerbutton + .spoiler .labelWrap label:before {
content: 'Verberg';
}
:target + .spoilerbutton + .spoiler .labelWrap .resetTarget {
cursor: default;
display: block;
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 100%;
}
:target + .spoilerbutton + .spoiler .listOfAnswer {
margin-top: 0;
}
.resetTarget {
  display: none;
}
<strong class="qTitle" id="question1">1. Question 1?</strong> 
<input type="checkbox" id="question1Checkbox" value="checked" class="spoilerbutton">
  <div class="spoiler">
    <div class="labelWrap">
      <label for="question1Checkbox"></label>
      <a class="resetTarget" href="#question1Checkbox"></a>
    </div>
    <div class="listOfAnswer">
      <ul>
        <li><a href="#question5">Open 5. link first</a></li>
      </ul>
    </div>
</div>
<strong class="qTitle" id="question2">2. Question 2?</strong> 
<input type="checkbox" id="question2Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler">
  <div class="labelWrap">
    <label for="question2Checkbox"></label>
    <a class="resetTarget" href="#question2Checkbox"></a>
  </div>
  <div class="listOfAnswer">
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>
<strong class="qTitle" id="question3">2. Question 3?</strong> 
<input type="checkbox" id="question3Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler">
  <div class="labelWrap">
    <label for="question3Checkbox"></label>
    <a class="resetTarget" href="#question3Checkbox"></a>
  </div>
  <div class="listOfAnswer">
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>
<strong class="qTitle" id="question4">2. Question 4?</strong> 
<input type="checkbox" id="question4Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler">
  <div class="labelWrap">
    <label for="question4Checkbox"></label>
    <a class="resetTarget" href="#question4Checkbox"></a>
  </div>
  <div class="listOfAnswer">
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>
<br>
<br>
<br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br><br>
<br>
<strong class="qTitle" id="question5">2. Question 5?</strong> 
<input type="checkbox" id="question5Checkbox" value="checked" class="spoilerbutton">
<div class="spoiler" id="answer5">
  <div class="labelWrap">
    <label for="question5Checkbox">
      <a class="resetTarget" href="#question5Checkbox"></a>
    </label>    
  </div>
  <div class="listOfAnswer">
    <ul>
      <li>possible answer 1.</li>
      <li>possible answer 2.</li>
      <li>possible answer 3.</li>
    </ul>
  </div>
</div>

【讨论】:

  • “我们不能使用 CSS 选中/取消选中复选框”是的,你可以,这是一种众所周知的纯 CSS 交互技术,通常被称为复选框 hack
【解决方案2】:

简答

一般来说,是的,可以做到这一点,但由于 CSS(Cascading Style Sheets)的限制,它有很大的局限性,所以实际上答案是 no强>。


长答案

我们面临的问题是,如果没有任何 JavaScript,您将无法动态地实施解决方案。您可以在您链接的可能的answer 中看到这一点。在纯 CSS 中,只能定位文件中指定的元素。 CSS如何知道要检查哪个checkbox(假设使用checkbox hack)或单击链接时要扩展哪个答案?答案很简单:不能

因此,在没有 JavaScript 的情况下,唯一的方法是不动态地进行。 问题是您无法定位父元素(Cascading 样式表),因此甚至没有纯 CSS 方式来打开另一个元素,例如,如果超链接位于 @987654327 中@ 并且您也不能使用 CSS 选中/取消选中复选框。

这使我们将 JavaScript 作为您代码结构的唯一可能解决方案。

.spoilerbutton {
  position: absolute;
  margin-top: 9px 0 0 -5px;
  height: 35px;
  width: 102px;
  opacity: 0;
}

.spoilerbutton:checked {
  width: 86px;
}

.spoilerbutton~.text::after {
  content: "Antwoord";
}

.spoilerbutton:checked~.text::after {
  content: "Verberg";
}

.spoilerbutton~.spoiler {
  display: none;
}

.spoilerbutton:checked~.spoiler {
  padding: 5px;
  display: block;
}
<div>
  <strong>1. Question 1?</strong>
  <p class="text"></p>
  <input class="spoilerbutton" type="checkbox" />

  <h2 class="text"></h2>

  <div class="spoiler">
    <div>
      <ul>
        <li><a onclick="document.querySelector(this.attributes['href'].value + ' .spoilerbutton').checked = true;" href="#answer3">Open answer 3</a></li>
        <li>possible answer 2.</li>
        <li>possible answer 3.</li>
      </ul>
    </div>
  </div>
</div>

<div>
  <strong>1. Question 2?</strong>
  <p class="text"></p>
  <input class="spoilerbutton" type="checkbox" />

  <h2 class="text"></h2>

  <div class="spoiler">
    <div>
      <ul>
        <li>possible answer 1.</li>
        <li>possible answer 2.</li>
        <li>possible answer 3.</li>
      </ul>
    </div>
  </div>
</div>

<div style="margin-top: 500px" id="answer3">
  <strong>1. Question 3?</strong>
  <p class="text"></p>
  <input class="spoilerbutton" type="checkbox" />

  <h2 class="text"></h2>

  <div class="spoiler">
    <div>
      <ul>
        <li>possible answer 1.</li>
        <li>possible answer 2.</li>
        <li>possible answer 3.</li>
      </ul>
    </div>
  </div>
</div>

一行代码就够了:

document.querySelector(this.attributes['href'].value + ' .spoilerbutton').checked = true;

编辑

实际上我忘记了:target 伪类。但是Hassan Siddiqui 已经完成了his answer。但是,您将不得不重新构建您的代码,如果您想使用 JavaScript 方法(我强烈推荐),我更喜欢我的代码行。

【讨论】:

  • 很好的解释和分析。我也喜欢使用querySelector 而不是getElementById 的方法,就像@Hassan Siddiqui 那样。
【解决方案3】:

我稍微简化了代码并创建了一个半工作的纯 CSS 选项。我无法让它完全按照您的要求工作,但我想有人可能仍然觉得它有用。

基本上我使用:focus psuedo 选择器在单击按钮时显示答案。

我还尝试使用:active 选择器来显示另一个答案中的答案。这不是很好,因为一旦元素不活动,:active 选择器就会消失。

尽管如此,这是一种在单击按钮时简单地显示答案的可能解决方案。

/* some quick styling */

.question {
  font-weight: bold;
  margin: 10px 0;
}

.answer {
  display: none;
  font-size: 14px;
  margin: 10px 0;
}

.show-answer {
  background: #cceedd;
}

.show-answer:focus {
  background: #cdcdcd;
}

a.answer-link {
  color: black;
  text-decoration: none;
}

a.answer-link span {
  color: blue;
  text-decoration: underline;
}

/* displaying the answers */

.show-answer:focus + .answer {
  display: block;
}

/* linking to an answer */

.answer-link--2:active ~ .answer--2 {
  display: block;
}
<div class="question">Question 1?</div>
<button class="show-answer show-answer--1">Show Answer</button>
<a class="answer answer--1 answer-link answer-link--2" href="#">This is the answer to Question 1. <span>Clicking and holding this text will open Question 2 answer, until you let go!.</span>.</a>
<hr/>
<div class="question">Question 2?</div>
<button class="show-answer show-answer--2">Show Answer</button>
<div class="answer answer--2">This is the answer to Question 2.</div>
<hr/>
<div class="question">Question 3?</div>
<button class="show-answer show-answer--3">Show Answer</button>
<div class="answer answer--3">This is the answer to Question 3.</div>
<hr/>
<div class="question">Question 4?</div>
<button class="show-answer show-answer--4">Show Answer</button>
<div class="answer answer--4">This is the answer to Question 4.</div>
<hr/>
<div class="question">Question 5?</div>
<button class="show-answer show-answer--5">Show Answer</button>
<div class="answer answer--5">This is the answer to Question 5.</div>

【讨论】:

    【解决方案4】:

    其他答案说明了在没有 JavaScript 的情况下解决此问题的缺点。

    在语义方面,我推荐使用details 元素。

    要从一个问题链接到另一个问题并同时显示该答案,请使用一个小脚本以编程方式更新 details 元素上的 [open] 属性。

    const section = document.getElementById('section');
    
    section.addEventListener('click', (e) => {
      const target = e.target;
      const isLink = target.classList.contains('link');
    
      if (!isLink) {
        return;
      }
    
      const href = target.getAttribute('href');
      const question = target.closest('section').querySelector(href);
    
      question.setAttribute('open', true);
    });
    body {
      height: 2000px;
    }
    
    summary {
      cursor: pointer;
    }
    <section id="section">
      <details id="q1">
        <summary>Question 1</summary>
        <ul>
          <li>possible answer 1.</li>
          <li>possible answer 2.</li>
          <li>see the answer to <a class="link" href="#q4">question 4</a></li>
        </ul>
      </details>
      <details id="q2">
        <summary>Question 2</summary>
        <ul>
          <li>possible answer 1.</li>
          <li>possible answer 2.</li>
          <li>possible answer 3.</li>
        </ul>
      </details>
      <details id="q3">
        <summary>Question 3</summary>
        <ul>
          <li>possible answer 1.</li>
          <li>possible answer 2.</li>
          <li>possible answer 3.</li>
        </ul>
      </details>
      <details id="q4">
        <summary>Question 4</summary>
        <ul>
          <li>possible answer 1.</li>
          <li>possible answer 2.</li>
          <li>possible answer 3.</li>
        </ul>
      </details>
    </section>

    【讨论】:

      【解决方案5】:

      以下演示是纯 HTML/CSS —— 不是一点 JavaScript。这包括许多忽略并且没有意识到它们是 JavaScript 的事件属性。举两个例子:

      &lt;button onclick="jsFunction(this); return false"&gt;X&lt;/button&gt;

      &lt;a href="#/" onclick="this.value=this.value=='Antwoord'?'Verberg':'Antwoord';"&gt;X&lt;/a&gt;

      每个问答都是一个隐藏的复选框,一个包含可见图例和标签的折叠字段集,以及作为隐藏内容的定义列表。 visibility: collapse/visible 用于该部分。

      我们使用:target 在跳转到目标字段集时打开它。所有用户交互和动画作品(甚至使用scroll-behavior: smooth 的滚动)都完全由以类为中心的 CSS 和战略性 HTML 布局。使用大量 &lt;br&gt; 或使用任何 JavaScript 都没有妥协。 CSS 中唯一使用的#id 是一个非必要的规则集,因此只要您坚持布局模式,并且类分配的扩展应该很轻松。

      更新
      HassanSiddiqui 提出了我对边缘案例的意图,请参阅下面的评论。我说边缘案例是因为我没有看到用户如此完全地关注常见问题解答,以至于他们需要返回并使用该特定链接。不过,为了完整起见,我已对其进行了以下更新:

      1. 问题 #5 的 label.tgr 最初是 display:none

      2. 取而代之的是a.tgr,它针对问题#5 的复选框input#act5

      3. 现在备份到问题 #1 中的链接,让我们回忆一下那里发生了什么:当点击该链接时,fieldset#t5c2 现在和永远是:target,这使得关闭变得困难,因为input#act5 未被选中。

      4. 所以#t5c2 是通过链接打开的,现在是:target。要从#t5c2 中删除该状态,请单击a.tgr 制作input#act5:target

      5. 所以此时a.tgr 将被label.tgr 替换并且#t5c2 被关闭。我不会详细说明我自己很困惑,所以只需查看 CSS。

      在全页模式下查看demo,初始紧凑模式下Snippet的iframe非常不成比例。

      html {
        scroll-behavior: smooth;
      }
      
      .main {
        height: 300vh;
        padding: 2vh 2vw 30vh;
      }
      
      .set {
        overflow: hidden;
        visibility: collapse;
        margin: 2vh 0;
      }
      
      .cat {
        max-height: 0;
        font-size: 0;
        opacity: 0;
      }
      
      :target .cat,
      .act:checked+.set .cat {
        max-height: 100vh;
        font-size: 1rem;
        opacity: 1.0;
        transition: opacity 1s, max-height 0.7s;
      }
      
      :target,
      .act:checked+.set {
        max-height: 100vh;
        visibility: visible;
        transition: 0.7s
      }
      
      .que,
      .tgr {
        visibility: visible;
        max-height: 50vh;
        color: #000;
        font-size: 1rem;
        text-decoration: none;
      }
      
      .tgr {
        cursor: pointer;
      }
      
      .que::before {
        content: attr(data-idx)'. ';
      }
      
      .lnx::after {
        content: ' 'attr(title);
      }
      
      
      /* Question 5 Switch Triggers */
      
      #act1:checked~.set .top {
        outline: 3px solid cyan
      }
      
      #act5:target+#t5c2 {
        visibility: collapse;
      }
      
      #act5:checked+#t5c2 {
        visibility: visible;
      }
      
      #t5c2 label.tgr {
        display: none;
      }
      
      #act5:target+#t5c2 label {
        display: inline-block;
      }
      
      #act5:target+#t5c2 a {
        display: none;
      }
      
      #act1:not(:checked)~#act5:not(:checked)+#t5c2:not(:target) a {
        display: none;
      }
      
      #act1:not(:checked)~#act5:not(:checked)+#t5c2:not(:target) label {
        display: inline-block;
      }
      <form class='main'>
      
        <input id='act1' class="act" type="checkbox" hidden>
        <fieldset class='set'>
          <legend class='que' data-idx='1'>Question</legend>
          <label class='tgr' for='act1'>Answers</label>
          <dl class='cat'>
            <dt>Category 1A</dt>
            <dd>Topic 1A1</dd>
            <dd>Topic 1A2</dd>
            <dt>Category 1B</dt>
            <dd>Topic 1B1
              <a href='#t5c2' class='lnx' title='Topic 5C2'>Also see</a>
            </dd>
            <dd>Topic 1B2</dd>
          </dl>
        </fieldset>
      
        <input id='act2' class="act" type="checkbox" hidden>
        <fieldset class='set'>
          <legend class='que' data-idx='2'>Question</legend>
          <label class='tgr' for='act2'>Answers</label>
          <dl class='cat'>
            <dt>Category 2A</dt>
            <dd>Topic 2A1</dd>
            <dd>Topic 2A2</dd>
            <dt>Category 2B</dt>
            <dd>Topic 2B1</dd>
          </dl>
        </fieldset>
      
        <input id='act3' class="act" type="checkbox" hidden>
        <fieldset class='set'>
          <legend class='que' data-idx='3'>Question</legend>
          <label class='tgr' for='act3'>Answers</label>
          <dl class='cat'>
            <dt>Category 3A</dt>
            <dd>Topic 3A1</dd>
            <dt>Category 3B</dt>
            <dd>Topic 3B1</dd>
            <dd>Topic 3B2</dd>
            <dd>Topic 3B3</dd>
          </dl>
        </fieldset>
      
        <input id='act4' class="act" type="checkbox" hidden>
        <fieldset class='set'>
          <legend class='que' data-idx='4'>Question</legend>
          <label class='tgr' for='act4'>Answers</label>
          <dl class='cat'>
            <dt>Category 4A</dt>
            <dd>Topic 4A1</dd>
            <dd>Topic 4A2</dd>
            <dd>Topic 4A3</dd>
          </dl>
        </fieldset>
      
        <input id='act5' class="act" type="checkbox" hidden>
        <fieldset id='t5c2' class='set'>
          <legend class='que' data-idx='5'>Question</legend>
          <label class='tgr' for='act5'>Answers</label>
          <a href='#act5' class='tgr'>Answers</a>
          <dl class='cat'>
            <dt>Category 5A</dt>
            <dd>Topic 5A1</dd>
            <dd>Topic 5A2</dd>
            <dt>Category 5B</dt>
            <dd>Topic 5B1</dd>
            <dd>Topic 5B2</dd>
            <dt>Category 5C</dt>
            <dd>Topic 5C1</dd>
            <dd class='top'>Topic 5C2</dd>
          </dl>
        </fieldset>
      
      </form>

      【讨论】:

      • 当我发现这是 HTML 中最简单的代码时,我授予了赏金。这非常重要,因为有很多问题。
      • 确实,简单性和功能性是保持代码稳定的关键。祝编码愉快!
      • @zer00ne - 它仅在您第一次单击 Also see Topic 5C2 时才起作用,它会向下滚动并展开答案 5,但是当用户单击 question 5 答案 title 时,它会折叠 question 5 all回答。然后向上滚动并再次单击Also see Topic 5C2,它会向下滚动以回答 5,但不会展开question 5 回答。
      • @HassanSiddiqui @HassanSiddiqui 这是:target 的本质,它会无限期地处于持续状态,除非刷新或点击了其他链接,请参阅更新。
      • 我不认为这是一个边缘案例,因为现在我们处于演示阶段,假设将来我们有多个问题和多个答案并且所有答案都相互关联,那么发生了什么?跨度>
      猜你喜欢
      • 1970-01-01
      • 2012-11-17
      • 2020-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多