简体   繁体   English

如何保存已切换的班级名称?

[英]How can I save my toggled class names?

I'm trying to make a function to toggle the language for my website. 我正在尝试创建一个功能来切换网站的语言。 I'm doing this by toggling classes with JavaScript, but if I reload the page or go to another it changes back to the original. 我通过使用JavaScript切换类来做到这一点,但是如果我重新加载页面或转到另一个页面,它会变回原始页面。 How can I save the changes that JavaScript makes? 如何保存JavaScript所做的更改?

 function langToggle() { if (document.getElementById("contenten").className == "show") { document.getElementById("contenten").className = "hide"; document.getElementById("contentnl").className = "show"; document.getElementById("myBtn").value = "Nederlands"; } else{ document.getElementById("contenten").className = "show"; document.getElementById("contentnl").className = "hide"; document.getElementById("myBtn").value = "English"; } } 
 .hide{ display: none; } .show{ display: block; } 
 <button id="myBtn" onclick="langToggle()" value="English">change language<i class="fa fa-globe"></i></button> <section id="contenten" class="show"> English </section> <section id="contentnl" class="hide"> Dutch </section> 

I only want to use JavaScript or PHP 我只想使用JavaScript或PHP

You questions is actually about sessionStorage (html5 feature, supported even on IE8!!!!). 您的问题实际上是关于sessionStorage(html5功能,即使在IE8上也受支持!!!!)。 More about this feature: https://developer.mozilla.org/en/docs/Web/API/Window/sessionStorage 有关此功能的更多信息: https : //developer.mozilla.org/en/docs/Web/API/Window/sessionStorage

In your case: 在您的情况下:

function langToggle() {
  var isNederlands = document.getElementById("contenten").className == "show";
  document.getElementById("myBtn").value = sessionStorage["lang"] = isNederlands ? "Nederlands" : "English";

  if (document.getElementById("contenten").className == "show") {
    document.getElementById("contenten").className = "hide";
    document.getElementById("contentnl").className = "show";

  } else{
    document.getElementById("contenten").className = "show";
    document.getElementById("contentnl").className = "hide";
  }

}

And then.. on your page Load event: 然后在您的页面上Load事件:

document.getElementById("myBtn").value = sessionStorage["lang"];

您可以在javascript中为此使用cookiessessionStorage

You could use localStorage (or sessionStorage , depending on whether you want it to be cleared when the browser closes), like this: 您可以使用localStorage (或sessionStorage ,具体取决于是否要在浏览器关闭时将其清除),如下所示:

// Update the display on page load
document.addEventListener("DOMContentLoaded", showLang);

function showLang() {
    // read the last saved language, defaults to 'en'
    var lang = localStorage.getItem('lang') || 'en';
    document.getElementById("contenten").className = lang == 'en' ? 'show' : 'hide';
    document.getElementById("contentnl").className = lang == 'nl' ? 'show' : 'hide';
    document.getElementById("myBtn").textContent = lang == 'nl' ? 'To English' : "Naar Nederlands";
}

function langToggle() {
    // read, toggle and update the language in local storage
    localStorage.setItem('lang', localStorage.getItem('lang') == 'en' ? 'nl' : 'en');
    // adapt the display accordingly
    showLang();
}

Note that the button does not have a value property, you need to use textContent instead. 请注意,该按钮没有value属性,您需要改用textContent

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

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