简体   繁体   English

我的 html 和 javascript 按钮无法更改语言

[英]my html and javascript button to change language doesn't work

I would like to know if any of you can help me and tell me why my code does not work.我想知道你们中是否有人可以帮助我并告诉我为什么我的代码不起作用。 What I want to do is that when I press the EN button, the language is changed to English and when I press the ES button, the language is changed to Spanish.我想要做的是,当我按下 EN 按钮时,语言变为英语,当我按下 ES 按钮时,语言变为西班牙语。 The problem is that the javascript code that I have made does not work.问题是我制作的javascript代码不起作用。

 const english = document.getElementById("en") const espanol = document.getElementById("es") const hey = document.getElementById("hey") const text = document.getElementById("text") english.addEventListener("click", function() { change(english, espanol); }); espanol.addEventListener("click", function() { change(espanol, english); }); function change(lang1, lang2) { if (lang1.innerHTML == "EN") { hey.innerHTML = "Hey, I'm English"; text.innerHTML = "I am a text in the English language"; } else { hey.innerHTML = "Hey, yo soy Español"; text.innerHTML = "Yo soy un texto en el idioma ingles "; } }
 .switch { position: relative; display: inline-block; margin: 0 5px; }.switch>span { position: absolute; top: 10px; pointer-events: none; font-family: 'Helvetica', Arial, sans-serif; font-weight: bold; font-size: 12px; text-transform: uppercase; text-shadow: 0 1px 0 rgba(0, 0, 0, .06); width: 50%; text-align: center; } input.check-toggle-round-flat:checked~.off { color: #000; } input.check-toggle-round-flat:checked~.on { color: #fff; }.switch>span.on { left: 0; padding-left: 2px; color: #000; }.switch>span.off { right: 0; padding-right: 4px; color: #fff; }.check-toggle { position: absolute; margin-left: -9999px; visibility: hidden; }.check-toggle+label { display: block; position: relative; cursor: pointer; outline: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } input.check-toggle-round-flat+label { padding: 2px; width: 100px; height: 35px; background-color: #facf0f; -webkit-border-radius: 60px; -moz-border-radius: 60px; -ms-border-radius: 60px; -o-border-radius: 60px; border-radius: 60px; } input.check-toggle-round-flat+label:before, input.check-toggle-round-flat+label:after { display: block; position: absolute; content: ""; } input.check-toggle-round-flat+label:before { top: 2px; left: 2px; bottom: 2px; right: 2px; background-color: #facf0f; -webkit- -moz-border-radius: 60px; -ms-border-radius: 60px; -o-border-radius: 60px; border-radius: 60px; } input.check-toggle-round-flat+label:after { top: 4px; left: 4px; bottom: 4px; width: 48px; background-color: #fff; -webkit-border-radius: 52px; -moz-border-radius: 52px; -ms-border-radius: 52px; -o-border-radius: 52px; border-radius: 52px; -webkit-transition: margin 0.2s; -moz-transition: margin 0.2s; -o-transition: margin 0.2s; transition: margin 0.2s; } input.check-toggle-round-flat:checked+label {} input.check-toggle-round-flat:checked+label:after { margin-left: 44px; }
 <div class="switch"> <input id="language-toggle" class="check-toggle check-toggle-round-flat" type="checkbox"> <label for="language-toggle"></label> <span class="on" id="en">EN</span> <span class="off" id="es">ES</span> </div> <h1 id="hey"> Hey, I'm English</h1> <div> <p id="text">I am a text in the English language </p> </div>

https://codepen.io/Skyance11441/pen/gOeQPJO https://codepen.io/Skyance11441/pen/gOeQPJO

You can use change event on the input field instead您可以在输入字段上使用change事件

 const english = document.getElementById("en") const espanol = document.getElementById("es") const projects = document.getElementById("hey") const contact = document.getElementById("text") const toggle = document.getElementById("language-toggle") toggle.addEventListener("change", function(e) { if (e.target.checked) { change(espanol, english); } else { change(english, espanol); } }) function change(lang1, lang2) { if (lang1.innerHTML == "EN") { hey.innerHTML = "Hey, I'm English"; text.innerHTML = "I am a text in the English language"; } else { hey.innerHTML = "Hey, yo soy Español"; text.innerHTML = "Yo soy un texto en el idioma ingles "; } }
 .switch { position: relative; display: inline-block; margin: 0 5px; }.switch>span { position: absolute; top: 10px; pointer-events: none; font-family: 'Helvetica', Arial, sans-serif; font-weight: bold; font-size: 12px; text-transform: uppercase; text-shadow: 0 1px 0 rgba(0, 0, 0, .06); width: 50%; text-align: center; } input.check-toggle-round-flat:checked~.off { color: #000; } input.check-toggle-round-flat:checked~.on { color: #fff; }.switch>span.on { left: 0; padding-left: 2px; color: #000; }.switch>span.off { right: 0; padding-right: 4px; color: #fff; }.check-toggle { position: absolute; margin-left: -9999px; visibility: hidden; }.check-toggle+label { display: block; position: relative; cursor: pointer; outline: none; -webkit-user-select: none; -moz-user-select: none; -ms-user-select: none; user-select: none; } input.check-toggle-round-flat+label { padding: 2px; width: 100px; height: 35px; background-color: #facf0f; -webkit-border-radius: 60px; -moz-border-radius: 60px; -ms-border-radius: 60px; -o-border-radius: 60px; border-radius: 60px; } input.check-toggle-round-flat+label:before, input.check-toggle-round-flat+label:after { display: block; position: absolute; content: ""; } input.check-toggle-round-flat+label:before { top: 2px; left: 2px; bottom: 2px; right: 2px; background-color: #facf0f; -webkit- -moz-border-radius: 60px; -ms-border-radius: 60px; -o-border-radius: 60px; border-radius: 60px; } input.check-toggle-round-flat+label:after { top: 4px; left: 4px; bottom: 4px; width: 48px; background-color: #fff; -webkit-border-radius: 52px; -moz-border-radius: 52px; -ms-border-radius: 52px; -o-border-radius: 52px; border-radius: 52px; -webkit-transition: margin 0.2s; -moz-transition: margin 0.2s; -o-transition: margin 0.2s; transition: margin 0.2s; } input.check-toggle-round-flat:checked+label {} input.check-toggle-round-flat:checked+label:after { margin-left: 44px; }
 <div class="switch"> <input id="language-toggle" class="check-toggle check-toggle-round-flat" type="checkbox"> <label for="language-toggle"></label> <span class="on" id="en">EN</span> <span class="off" id="es">ES</span> </div> <h1 id="hey"> Hey, I'm English</h1> <div> <p id="text">I am a text in the English language </p> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM