简体   繁体   English

修改外部内容<div>和<select>, 但没有 JavaScript

[英]Modify content of an external <div> with <select>, but without JavaScript

I have an HTML page which looks something like this,我有一个看起来像这样的 HTML 页面,

<select id="selectMe">
  <option value="option1">Question 1</option>
  <option value="option2">Question 2</option>
  <option value="option3">Question 3</option>
</select>
<div id="option1">Answer 1</div>
<div id="option2">Answer 2</div>
<div id="option3">Answer 3</div>

I want to use the <select> element to choose which answer <div> to show.我想使用<select>元素来选择要显示的答案<div> Unfortunately, I cannot use any JavaScript and must do this with HTML / CSS only.不幸的是,我不能使用任何 JavaScript,只能使用 HTML/CSS 来做到这一点。

Also note I'm not hell-bent on using <select> if some other form elements will make this easier.另请注意,如果其他一些表单元素会使这变得更容易,我并不会一味地使用<select> But not using any JavaScript is a hard restriction for me.但是不使用任何 JavaScript 对我来说是一个严格的限制。

I mean there is one-way (not totally proud of it) but using radio buttons help us to track checked state using the :checked pseudo-selector.我的意思是有一种方法(并不完全为此感到自豪)但是使用radio按钮可以帮助我们使用:checked伪选择器来跟踪checked状态。 We can use the same to map from question1 to option1 and so on.我们可以使用相同的方法从question1映射到option1等等。

 .options{ display:none; } #question1:checked ~ #option1, #question2:checked ~ #option2, #question3:checked ~ #option3{ display:block; }
 <input type="radio" name="questions" id="question1" checked/> <label for="question1">Quesion 1</label> <input type="radio" name="questions" id="question2"/> <label for="question2">Question 2</label> <input type="radio" name="questions" id="question3"/> <label for="question3">Question 3</label> <div id="option1" class="options">Answer 1</div> <div id="option2" class="options">Answer 2</div> <div id="option3" class="options">Answer 3</div>

I cannot think of a way to hide elements without JS, however, you could use page anchors.我想不出一种不用 JS 来隐藏元素的方法,但是,您可以使用页面锚点。

  1. Make the divs match the height of the screen使 div 与屏幕高度匹配
  2. Use anchors to link to the divs on page使用锚链接到页面上的 div
  3. Fix the nav to the top of the screen so that on scrolling it doesn't disappear将导航固定到屏幕顶部,以便在滚动时它不会消失
  4. Disable the user scrolling which would make it appear to hide/show the divs.禁用用户滚动,这将使它看起来隐藏/显示 div。

Hope this helps.希望这可以帮助。

 <div class="sticky"> <a href="#option1">Question 1</a> <a href="#option2">Question 2</a> <a href="#option3">Question 3</a> </div> <div id="option1" class="full-height blue">Answer 1</div> <div id="option2" class="full-height green">Answer 2</div> <div id="option3" class="full-height red">Answer 3</div> <style> body { overflow: hidden; margin: 0; } .sticky { position: fixed; background: white; } .full-height { color: white; display: flex; justify-content: center; align-items: center; height: 100vh; } .blue { background: blue; } .green { background: green; } .red { background: red; } </style>

I think you can use the CSS CheckBox Hack .我认为您可以使用 CSS CheckBox Hack You will however need to build your own <select> because unfortunately it is not possible to put HTML tags inside an <option> .但是,您将需要构建自己的<select>因为不幸的是,不可能将 HTML 标记放在<option>

The basic idea is outlined below.基本思想概述如下。 On the linked page you can also find an example for how to hide and show content.在链接页面上,您还可以找到有关如何隐藏和显示内容的示例。

 .visually-hidden { position: absolute; left: -100vw; } .dropdown-header::after { content: "> Options (Click to expand)"; } #option1:checked ~ .dropdown-header::after { content: "Question 1"; } #option2:checked ~ .dropdown-header::after { content: "Question 2"; } #option3:checked ~ .dropdown-header::after { content: "Question 3"; } .answer::after { content: ""; } #option1:checked ~ .answer::after { content: "Answer 1"; } #option2:checked ~ .answer::after { content: "Answer 2"; } #option3:checked ~ .answer::after { content: "Answer 3"; } .dropdown:hover { background-color: blue; } #dropdown:checked ~ .dropdown { display: inline; } .dropdown { display: none; }
 <div> <input type="checkbox" class="visually-hidden" id="dropdown"> <label for="dropdown" class="dropdown-header"></label><br> <label class="dropdown" for="option1">Question 1</label><input type="radio" id="option1" name="rad" class="visually-hidden" checked><br> <label class="dropdown" for="option2">Question 2</label><input id="option2" type="radio" class="visually-hidden" name="rad"/><br> <label class="dropdown" for="option3">Question 3</label><input id="option3" type="radio" class="visually-hidden" name="rad"/><br> <div class="answer"></div> </div>

Like it was mentioned before the best way is to use inputs with types radio or checkbox depending on what you want to achieve.就像之前提到的那样,最好的方法是根据您想要实现的目标使用带有 radio 或 checkbox 类型的输入。 The example of using radio buttons and labels is here https://codepen.io/alekskorovin/pen/abBeqzN使用单选按钮和标签的例子在这里https://codepen.io/alekskorovin/pen/abBeqzN

 .answer-toggle, .answer { display: none; } .answer-toggle:checked ~ .answer, .question { display: block; }
 <label class="question" for="answer1">Question 1</label> <label class="question" for="answer2">Question 2</label> <label class="question" for="answer3">Question 3</label> <div> <input class="answer-toggle" type="radio" name="answer" id="answer1"> <div class="answer">Answer 1</div> </div> <div> <input class="answer-toggle" type="radio" name="answer" id="answer2"> <div class="answer">Answer 2</div> </div> <div> <input class="answer-toggle" type="radio" name="answer" id="answer3"> <div class="answer">Answer 3</div> </div>

if you remove the same name="answer" then each answer could be shown independently.如果您删除相同的 name="answer" 那么每个答案都可以独立显示。

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

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