简体   繁体   English

如何将 Div 显示 CSS 从无更改为阻止按钮单击

[英]How To Change Div Display CSS From None To Block On Button click

I am trying to make some quiz lander.我正在尝试制作一些测验着陆器。 And I want to change div cs from display: None to Display: block on click of a button.我想将 div cs 从 display: None 更改为 Display: block on click a button。

I am trying it using JS but I am not able to.我正在使用 JS 尝试它,但我无法做到。

example: I have questions and two buttons per question (Yes and No) So if you click on any button (either yes or no), then Div 2 must change its CSS from display: none to display: Block and So On For other questions.例如:我有问题,每个问题有两个按钮(是和否)因此,如果您单击任何按钮(是或否),则 Div 2 必须将其 CSS 从 display: none 更改为 display: Block and So On 对于其他问题.

Hope You Are Getting it.希望你得到它。

Here is the link to tet post I am trying to do这是我正在尝试做的 tet post 的链接

https://us.healthshul.com/test-post/ https://us.healthshul.com/test-post/

Guys need some help.伙计们需要一些帮助。

lets say you have this button and text:假设你有这个按钮和文本:

<div id="foo">hello world!</div>
<button id="switch" />Click me</button>

use this code to change the css:使用此代码更改 css:

$('#switch').click(function() {
    $('#foo').css({
        'display': 'Block'
    });
});

this way when you click switch the style display will change from None to Block这样,当您单击 switch 时,样式显示将从 None 更改为 Block

Load all your questions at once.一次加载所有问题。 Position them in the same place and use position:absolute to allow them to overlap. Position 将它们放在同一个地方并使用position:absolute让它们重叠。 Give them ID values that increase by 1 each question.给他们每个问题增加 1 的 ID 值。

The JavaScript I've written sets a variable that corresponds with the question numbers (which are also the question IDs).我编写的 JavaScript 设置了一个与问题编号(也是问题 ID)相对应的变量。 The function executes when the user clicks an answer. function 在用户单击答案时执行。 It retrieves the current question's ID, sets its display to none (revealing the next question), then adds one to the ID variable's value.它检索当前问题的 ID,将其显示设置为无(显示下一个问题),然后将 ID 变量的值加一。

Here is the code:这是代码:

 var num = 1; function display() { document.getElementById(num).style.display = "none"; num += 1; }
 .button { padding: 10px 20px; border: 1px solid black; }.button:hover { cursor: pointer; }.question { width: 250px; height: 100px; background-color: gray; display: block; position: absolute; }
 <div class="question" id="4"> <span class="text">Question4 Question4 Question4 Question4 Question4 Question4</span> <button class="button" onClick="display();">Answer 1</button> <button class="button" onClick="display();">Answer 2</button> </div> <div class="question" id="3"> <span class="text">Question3 Question3 Question3 Question3 Question3 Question3</span> <button class="button" onClick="display();">Answer 1</button> <button class="button" onClick="display();">Answer 2</button> </div> <div class="question" id="2"> <span class="text">Question2 Question2 Question2 Question2 Question2 Question2</span> <button class="button" onClick="display();">Answer 1</button> <button class="button" onClick="display();">Answer 2</button> </div> <div class="question" id="1"> <span class="text">Question Question Question Question Question Question Question</span> <button class="button" onClick="display();">Answer 1</button> <button class="button" onClick="display();">Answer 2</button> </div>

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

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