gpt4 book ai didi

javascript - 如何使用 Javascript 创建显示更多或显示更少的可切换按钮?

转载 作者:行者123 更新时间:2023-11-30 13:46:17 27 4
gpt4 key购买 nike

我在尝试使我的代码可切换时遇到问题。我可以按下按钮,它会显示隐藏的任何内容,但是,当我再次尝试按下按钮时,它不会隐藏内容。我在这里做错了什么?

let toggle = document.querySelector('#chevron');

toggle.addEventListener("click", function(event) {
event.preventDefault()

let press = toggle.value;

if (press = "fas fa-chevron-right") {
document.getElementById("content").classList = '';
document.getElementById("chevron").innerHTML = 'SHOW LESS <i class="fas fa-chevron-down"></i>';
} else if (press = "fas fa-chevron-down") {
document.getElementById("content").classList = 'hidden';
document.getElementById("chevron").innerHTML = 'MORE <i class="fas fa-chevron-right"></i>';
}

});
.hidden {
display: none;
}

a {
text-decoration: none;
}
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<meta content="ie=edge" http-equiv="X-UA-Compatible">
<link href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.11.2/css/all.css" rel="stylesheet">
<link href="styles.css" rel="stylesheet">
<script defer src="main.js"></script>
</head>

<body>
<a href="#" id="chevron" onclick="toggle"> SHOW MORE <i class="fas fa-chevron-right"></i></a>
<div id="content" class="hidden">
<div>
<ul>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
<li>Lorem Ipsum</li>
</ul>
</div>
</div>
</body>

</html>

最佳答案

您可以使用 <details> HTML 元素。

The HTML Details Element (<details>) creates a disclosure widget in which information is visible only when the widget is toggled into an "open" state. A summary or label can be provided using the <summary> element.

详细信息元素 here .


示例:

<details>
<summary>Copyright 1999-2018.</summary>
<p> - by Refsnes Data. All Rights Reserved.</p>
<p>All content and graphics on this web site are the property of the company Refsnes Data.</p>
</details>

关于javascript - 如何使用 Javascript 创建显示更多或显示更少的可切换按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59260982/

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