我在网页的右边有一个列表,有4个按钮,当点击一个按钮时,就会显示它的相关内容;这是通过JavaScript实现的。EventListener运行一个函数来切换类“active”。
按钮HTML元素还有一个onclick = myFunciton(),它链接到JS以隐藏右箭头图标。
我想设置为一次只能打开其中一个内容版块,如何实现?
以下是我的HTML代码:
<div class="container-fluid">
<div class="sec2">
<h1>A 44-letter alphabet.</h1>
<h1>A phonetically consistent language.</h1>
<h1>A determined teacher you can count on.</h1>
</div>
<div class="sec3">
<ul class="sec2drop">
<li>
<button onclick="myFunction()" class="collapsible" id="colly1">
Prepare for a language exam
<i class="fa fa-caret-right" id="arrow0"></i>
</button>
<div class="content">
<p>
A hundred different reasons, one perfect outcome. Maybe you
were born to Hungarian parents abroad, and decided to take a
Hungarian language exam to replace one of your high school
leaving exams.
</p>
<p>
Maybe you're going to be an exchange student in Hungary or
work there for some time. I'm happy to prepare you in video
call sessions, both with verbal and written training.
</p>
</div>
</li>
<li>
<button onclick="myFunction()" class="collapsible" id="colly2">
Hungarian for kids
<i class="fa fa-caret-right" id="arrow1"></i>
</button>
<div class="content">
<p>
Many Hungarians living abroad would like to teach their
linguistics or national heritage. I will tailor online
sessions for your child that suites their and your needs the
best.
</p>
<p>
You decide if you would like to supervise or participate in
the lesson. Generally, children under 8 require parental or
older sibling presence.
</p>
</div>
</li>
<li>
<button onclick="myFunction()" class="collapsible" id="colly3">
Take on a challenge
<i class="fa fa-caret-right" id="arrow2"></i>
</button>
<div class="content">
<p>
Whether you spontaneously decided to learn one of the hardest
languages in the world, or you want to reconnect with your
Hungarian heritage, I will guide you along the way. Are you a
newbie or an advanced learner? Would you like specific types
of homework in between lessons? I will adapt to your needs.
</p>
</div>
</li>
<li>
<button onclick="myFunction()" class="collapsible" id="colly4">
Translation services
<i class="fa fa-caret-right" id="arrow3"></i>
</button>
<div class="content">
<p>
English to Hungarian or Hungarian to English. You can book my
written translation services and choose the package most
suitable to you.
</p>
</div>
</li>
</ul>
</div>
</div>
以下是我的CSS代码:
/* Style the button that is used to open and close the collapsible content */
.collapsible {
background-color: transparent;
cursor: pointer;
padding: 18px;
margin-left: 10px;
border: none;
font-size: 15px;
width: 100%;
display: flex;
justify-content: space-between;
}
/* Style the collapsible content. Note: hidden by default */
.content {
padding: 0px 18px;
display: none;
overflow: hidden;
background-color: wheat;
}
以下是我的JavaScript代码:
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function () {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});
}
function myFunction() {
for (y = 0; y < 4; y++) {
var x = document.getElementById(`arrow${y}`);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
}
3条答案
按热度按时间mfuanj7w1#
你可以通过折叠所有的活动元素来实现这一点。检查collapseAll()函数。
8cdiaqws2#
您的网站非常适合使用The Details disclosure element。
<details><summary>
元素已经包含了一个'open'/'close'机制,因此不需要用Javascript创建:<details>
元素的<summary>
元素时,该机制会将 *boolean属性 *open
添加到<details>
元素中,并显示<summary>
下面的内容。在连续单击时,该属性会被删除,详细信息再次关闭,隐藏内容。.content
)* 保持打开 *,直到用户再次关闭它们。由于您希望一次只打开一个
.content
项目,因此需要简单的Javascript功能来关闭已打开的<details>
,然后在用户单击时打开下一个项目。在代码片段中,我将
.sec3
的内容更改为与<details><summary>
一起使用,并添加了前面提到的简单Javascript来切换.content
的可见性。我删除了您的 FontAwesome 图标,并使 “Prepare for a language exam” 的summary
与 FontAwesome 一起使用,而其他三个细节使用HTML默认的插入符号。这样您就可以看到差异并选择保留哪一个。“准备语言考试” 还展示了如何在需要时嵌套<details><summary>
。代码片段已注解,且应不言自明:
r8uurelv3#
使用inline event listeners可能不是最好的主意。这里有一个最小的例子,使用event delegation,data-attributes来识别需要展开的内容,并使用css类来激活要展开的元素。