1 回答
TA贡献1785条经验 获得超4个赞
无论页面上有多少元素,这都有效:
function showHideEnglish() {
var german = document.getElementsByClassName("text__german");
for (var i = 0; i < german.length; i++) {
german[i].style.display = "none";
}
var english = document.getElementsByClassName("text__english");
for (var i = 0; i < english.length; i++) {
english[i].style.display = "block";
}
}
function showHideGerman() {
var german = document.getElementsByClassName("text__german");
for (var i = 0; i < german.length; i++) {
german[i].style.display = "block";
}
var english = document.getElementsByClassName("text__english");
for (var i = 0; i < english.length; i++) {
english[i].style.display = "none";
}
}
<button onclick="showHideEnglish();">English</button>
<button onclick="showHideGerman();">German</button>
<div class="text__english" style="display:block;">This text is English</div><br>
<div class="text__german" style="display:none;">dieser Text ist auf Deutsch</div><br>
<div class="text__english" style="display:block;">This text is English</div>
<div class="text__german" style="display:none;">dieser Text ist auf Deutsch</div>
添加回答
举报