Я выбрал некоторый код из;
https://www.w3schools.com/howto/howto_js_collapsible.asp
Как бы я добавил
id = "link2"
к каждому разборному элементу,
, чтобы раскрытие разворачивалоськ содержанию, когда я получаю к нему доступ с помощью якорной ссылки.
Например:
AnimatedCollapsible.html # link2
Я прочитал некоторые из других расширенных ссылоктемы по SO,
, но мне трудно понять, как заставить работать расширяющуюся ссылку.
Спасибо за любые предложения.
<!DOCTYPE html>
<html>
<head>
<style>
.collapsible {
background-color: #777;
color: white;
cursor: pointer;
padding: 18px;
width: 100%;
border: none;
text-align: left;
outline: none;
font-size: 15px;
}
.active, .collapsible:hover {
background-color: #555;
}
.collapsible:after {
content: '\002B';
color: white;
font-weight: bold;
float: right;
margin-left: 5px;
}
.active:after {
content: "\2212";
}
.content {
padding: 0 18px;
max-height: 0;
overflow: hidden;
transition: max-height 0.2s ease-out;
background-color: #f1f1f1;
}
</style>
</head>
<body>
<button class="collapsible">Open Section 1</button><div class="content"><p>1.</p></div>
<button class="collapsible">Open Section 2</button><div class="content"><p>2.</p></div>
<button class="collapsible">Open Section 3</button><div class="content"><p>3.</p></div>
<script>
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.maxHeight){
content.style.maxHeight = null;
} else {
content.style.maxHeight = content.scrollHeight + "px";
}
});
}
</script>
</body>
</html>