javascript - 简单函数只能运行一次(显示/隐藏 div) - vanilla JavaScript

标签 javascript html

我正在开发一个多语言网站。

我希望能够按下一个按钮,并且所有(包装内容)div 的显示变为,并且仅显示正确的一个 变成 inline-block

它有效。但只有一次。问题是什么?请不要使用 JQuery。

编辑向 html 添加了一些代码,使其“易于理解”

JS(toSl、toAn 等所有函数都是相同的)。

function toRu () {
    var lngs, i;
    lngs = document.querySelectorAll(".lng");
    for (var i=0;i<lngs.length;i++){
        lngs[i].style.display="none";
    }
    document.getElementById("ru").style.display="inline-block";
}

document.getElementById("slo").addEventListener("click", toSl);
document.getElementById("ang").addEventListener("click", toAn);
document.getElementById("nem").addEventListener("click", toNe);
document.getElementById("ita").addEventListener("click", toIt);
document.getElementById("hrv").addEventListener("click", toHr);
document.getElementById("rus").addEventListener("click", toRu);

HTML

<!DOCTYPE html>
<html>
<body>
<div class="lng" id="sl">
<nav>
<div id="lngSpace">
    Language Selector
    <input type="button" id="slo" class="zastave" value="Slo">
    <input type="button" id="ang" class="zastave" value="Eng">
    <input type="button" id="nem" class="zastave" value="Deu">
    <input type="button" id="ita" class="zastave" value="Ita">
    <input type="button" id="hrv" class="zastave" value="Hrv">
    <input type="button" id="rus" class="zastave" value="Rus">
</div>
</nav>
Content of the page in Sl language
</div>

<div class="lng" id="en">
<nav>
<div id="lngSpace">
    Language Selector
    <input type="button" id="slo" class="zastave" value="Slo">
    <input type="button" id="ang" class="zastave" value="Eng">
    <input type="button" id="nem" class="zastave" value="Deu">
    <input type="button" id="ita" class="zastave" value="Ita">
    <input type="button" id="hrv" class="zastave" value="Hrv">
    <input type="button" id="rus" class="zastave" value="Rus">
</div>
</nav>
Content of the page in En language
</div>

<div class="lng" id="de">
<nav>
<div id="lngSpace">
    Language Selector
    <input type="button" id="slo" class="zastave" value="Slo">
    <input type="button" id="ang" class="zastave" value="Eng">
    <input type="button" id="nem" class="zastave" value="Deu">
    <input type="button" id="ita" class="zastave" value="Ita">
    <input type="button" id="hrv" class="zastave" value="Hrv">
    <input type="button" id="rus" class="zastave" value="Rus">
</div>
</nav>
Content of the page in Ger language
</div>

etc etc

</body>
</html>

编辑我的代码还包含这段在最开始运行的JS。我不认为它会影响 JS 的其余部分,因为我只想更改 div 的显示而不重新加载页面...但我只是将其发布以防万一(就像发布到 plnkr 的示例一样)有效...)

var language = navigator.language || navigator.languages[0];
console.log(language);
var languageFistTwo = language.substr(0,2); // To only keep the first 2 characters.
console.log(languageFistTwo);

switch (languageFistTwo) {
    case "sl":
        document.getElementById("sl").style.display="inline-block";
        break;  
    case "en":
        document.getElementById("en").style.display="inline-block";
        break;
    case "de":
        document.getElementById("de").style.display="inline-block";
        break;
    case "it":
        document.getElementById("it").style.display="inline-block";
        break;
    case "hr":
        document.getElementById("hr").style.display="inline-block";
        break;
    case "ru":
        document.getElementById("ru").style.display="inline-block";
        break;
    default:
        document.getElementById("en").style.display="inline-block";
        break;
}

示例:https://plnkr.co/edit/FWsUfBzRp1yuS4WKDTIw?p=info

最佳答案

这是我假设您正在尝试做的事情的编辑示例。如果你想运行你的示例,你必须在按钮上使用 class 而不是 id 和所有图像的 click 监听器,而不仅仅是第一组。

无论哪种方式,您都可以在一个函数中解决所有问题。您不需要为每种语言提供一个函数。

注释和解释在代码中。

//function toRu(){
function toAny(){
    var lngs = document.querySelectorAll(".lng");
    for (var i=0;i<lngs.length;i++){
        //REM: The element with the matching id is inline-block, the others none
        lngs[i].style.display = lngs[i].getAttribute('data-id') == this.value ? 'inline-block' : 'none'
    }
}

//REM: This only assings the event to one button
/*
document.getElementById("slo").addEventListener("click", toAny);
document.getElementById("ang").addEventListener("click", toAny);
document.getElementById("nem").addEventListener("click", toAny);
document.getElementById("ita").addEventListener("click", toAny);
document.getElementById("hrv").addEventListener("click", toAny);
document.getElementById("rus").addEventListener("click", toAny);
*/

for(var tL=document.querySelectorAll('input.zastave'), i=0, j=tL.length; i<j; i++){
  tL[i].addEventListener("click", toAny);
}
<!--
I do not know why you replicated the language selectors multiple times. For me this structure makes more sense.
-->
<nav>
  <input type="button" id="slo" class="zastave" value="Slo">
  <input type="button" id="ang" class="zastave" value="Eng">
  <input type="button" id="nem" class="zastave" value="Deu">
  <input type="button" id="ita" class="zastave" value="Ita">
  <input type="button" id="hrv" class="zastave" value="Hrv">
  <input type="button" id="rus" class="zastave" value="Rus">
</nav>
<!--
id should be unique. Therefore I changed it to data-id and made it match the values of the buttons. This allows us to easily toggle the right divs in one function
-->
<div class="lng" data-id="Slo">
Content of the page in Sl language
</div>

<div class="lng" data-id="Eng">
Content of the page in En language
</div>

<div class="lng" data-id="Deu">
Content of the page in Ger language
</div>

关于javascript - 简单函数只能运行一次(显示/隐藏 div) - vanilla JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59787499/

相关文章:

javascript - 如何禁用数据表中的键绑定(bind)

javascript - Vue2 过渡模式不起作用

javascript - 隐藏/显示不同的链接

html - Tumblr 托管的视频流体宽度和高度

html - 垂直 CSS 进度条

javascript - nock:如何模拟带有附加 header 的请求?我得到诺克:请求不匹配

javascript - Vue.js 路由从子目录服务

php - 如何使用 PHP、HTML 等从多个页面中选择项目?

html - CSS Fliping 动画在 IE 9 或 10 中不起作用

PHP:表示查询结果