gpt4 book ai didi

html - 如何仅通过单击单选按钮使网站的一部分不隐藏

转载 作者:行者123 更新时间:2023-12-05 00:51:00 33 4
gpt4 key购买 nike

我隐藏了两部分代码并创建了两个单选按钮。

如果选择单选按钮 1,我想取消隐藏第一部分,但在选择单选按钮 2 时再次隐藏它并取消隐藏第二个隐藏部分。反之亦然,无需使用提交按钮。

我查看了有关单选按钮和/或隐藏和取消隐藏 html 代码的其他问题,但大多数时候来自提问者的代码非常长,我看不到完整的图片。

    <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>How to make a part of the website unhidden by only clicking a radio button</title>
</head>
<body>
<!-- Radio Buttons to choose which part should be visible -->
<input type="radio" id="option_1" name="options" value="option_1">
<label for="option_1">Option 1</label><br>
<input type="radio" id="option_2" name="options" value="option_2">
<label for="option_2">Option 2</label><br><br>

<!-- Hidden part #1 -->
<div hidden class="hidden_part_1">This turns visible by selecting radio button with Option 1</div>

<!-- Hidden part #1 -->
<div hidden class="hidden_part_2">This turns visible by selecting radio button with Option 2</div>
</body>
</html>

最佳答案

使用 onclick 可以运行一个 JS 函数,将对象设置为可见或不可见。

添加了 JS 和 onclick 功能来切换可见的内容。

const part1 = document.querySelector(".hidden_part_1");
const part2 = document.querySelector(".hidden_part_2");

part1.style.display = "none";
part2.style.display = "none";

function button1() {
part1.style.display = "block";
part2.style.display = "none";
}

function button2() {
part1.style.display = "none";
part2.style.display = "block";
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<title>How to make a part of the website unhidden by only clicking a radio button</title>
</head>

<body>
<!-- Radio Buttons to choose which part should be visible -->
<input type="radio" id="option_1" name="options" value="option_1" onclick="button1()">
<label for="option_1">Option 1</label><br>
<input type="radio" id="option_2" name="options" value="option_2" onclick="button2()">
<label for="option_2">Option 2</label><br><br>

<!-- Hidden part #1 -->
<div class="hidden_part_1">This turns visible by selecting radio button with Option 1</div>

<!-- Hidden part #1 -->
<div class="hidden_part_2">This turns visible by selecting radio button with Option 2</div>
</body>

</html>

关于html - 如何仅通过单击单选按钮使网站的一部分不隐藏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73267582/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com