我想使用一种可以隐藏或显示不同div(无/块)的javascript函数。但是对于单击的每个不同按钮,应该显示或隐藏一个不同的div。
我的功能如下:
function visibility() {
var x = document.getElementById("searchfield");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
按钮是:
<div><button id="subclass_button" onclick="visibility()">Age</button>
<div><button id="subclass_button" onclick="visibility()">Death Place</button>
<div><button id="subclass_button" onclick="visibility()">Death Cause</button>
<div><button id="subclass_button" onclick="visibility()">Music Genre</button>
<div><button id="subclass_button" onclick="visibility()">Gender</button>
<div><button id="subclass_button" onclick="visibility()">Birthplace</button>
我要显示/隐藏的div如下,必须链接到一个按钮:
<div id="gender">
<button>Male</button>
<button>Female</button>
</div>
<div id="deathPlace">
<input type="text" class="form-control" placeholder="Search for a death place..." >
<button ng-click="doMyAction()" type="button">Search</button>
</div>
<div id="birthplace">
<input type="text" class="form-control" placeholder="Search for a birthplace..." >
<button ng-click="doMyAction()" type="button">Search</button>
</div>
因此,如果我按性别按钮,则只希望显示id =“ gender”的div。 我希望有人能帮帮忙!预先感谢。
答案 0 :(得分:0)
只需将目标div的ID发送给您的函数
<div><button id="subclass_button" onclick="visibility('gender')">Gender</button>
然后在您的函数中
function visibility(divId) {
var x = document.getElementById(divId);
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}