为什么我需要在我的“a”标签上点击两次来运行 onclick 事件?我的目标:当“a”标签被点击时,显示为块下面默认隐藏的div。
function myFunction1(num) {
var x = document.getElementById("description");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";;
}
}
function myFunction2(num) {
var x = document.getElementById("benefits");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";;
return
}
}
function myFunction(button) {
var x = button.id;
switch (x) {
case '1':
myFunction1(x);
break;
case '2':
myFunction4(x);
break;
default:
return false;
}
}
var buttons = document.getElementsByTagName('a');
for (var i = 0, len = buttons.length; i < len; i++) {
buttons[i].onclick = function() {
myFunction(this);
}
};
<header class="container">
<nav class="navbar">
<a id="1">Description</a>
<a id="2">Benefits</a>
</nav>
</header>
<div class="details">
<div class="details_object" id="description" style="display: none">
<p> <span></span> Lorem ipsum dolor sit, amet consectetur adipisicing elit. Illo, dolorem. </p>
<p> <span></span> Lorem ipsum dolor sit, amet consectetur adipisicing elit. Illo, dolorem. </p>
</div>
<div class="details_object" id="benefits" style="display: none">
<p> <span></span> Lorem ipsum dolor sit, amet consectetur adipisicing elit. Illo, dolorem. </p>
<p> <span></span> Lorem ipsum dolor sit, amet consectetur adipisicing elit. Illo, dolorem. </p>
</div>
我还尝试实现的是一次只显示一个 .details_object,所以如果显示了一个,但调用了另一个函数,请将第一个设置为默认值。
www说
白衣染霜花
相关分类