【问题标题】:Javascript Local storage with function?带有功能的Javascript本地存储?
【发布时间】:2021-11-25 07:14:28
【问题描述】:

我有一个翻译功能,只需单击一个按钮,即可使用 javascript 将段落从英文切换为法文。现在我使用 innerhtml 手动抓取预翻译的段落。我想使用 localStorage 存储用户操作。如果他们点击翻译 onclick 事件,它应该自动在每个其他页面上运行 translate_fr 函数。我想避免用户每次加载新页面时都必须重新翻译网站。

我不知道如何使用某种条件语句来执行此操作选择使用 localstorage 存储在浏览器中。

 function translate_fr(){
 
 document.getElementById("intro").innerHTML = `Chez Coupe Lion, nous ne sommes qu'un chat établissement offrant une gamme complète de services du toilettage complet, de la baignade à l'embarquement.Vous et votre animal sera ravi de savoir que seul un professionnel, des produits naturels et biodégradables sont utilisés, tout les sensibilités ou les allergies ne seront pas un problème`;
 
}
<button class="translate" type="button" onclick="translate_fr()">FR</button>

    <p id="intro">
      Here at Lion Kuts we are a cat only
      establishment that offers a full range of services 
      from complete grooming, bathing to boarding. You and 
      your pet will be thrilled to know that only professional, 
      natural and biodegradeable products are used, any 
      sensitivities or allergies will not be a problem.
  </p>

【问题讨论】:

  • 您在第一段中所说的内容相当混乱。你能改写一下吗?
  • 如果默认是英文,只需在用户点击按钮时执行localStorage.setItem("french", "yes");。然后在他们切换回英语时执行localStorage.removeItem("french");。并且在每个页面上,加载后,只需检查是否localStorage.getItem("french") === "yes",如果为真则翻译页面。
  • 为了简单起见,用户第一次进入网站并选择将页面翻译成法语。现在他转到第二页,该翻译应该延续到下一页。默认情况下,所有页面都以英文加载。如果他们选择在某一点激活它,我只希望 translate_fr 函数在其他页面上运行
  • 我认为 Chris G 发布的内容正是我要寻找的内容

标签: javascript html function local-storage


【解决方案1】:

你可以试试这样的:

function translate_fr() {
  document.getElementById(
    "intro"
  ).innerHTML = `Chez Coupe Lion, nous ne sommes qu'un chat établissement offrant une gamme complète de services du toilettage complet, de la baignade à l'embarquement.Vous et votre animal sera ravi de savoir que seul un professionnel, des produits naturels et biodégradables sont utilisés, tout les sensibilités ou les allergies ne seront pas un problème`;
  localStorage.setItem("isFrench", "1");
}

function translate_eng() {
  document.getElementById("intro").innerHTML = `Here at Lion Kuts we are a cat only
  establishment that offers a full range of services 
  from complete grooming, bathing to boarding. You and 
  your pet will be thrilled to know that only professional, 
  natural and biodegradeable products are used, any 
  sensitivities or allergies will not be a problem.`;
  localStorage.setItem("isFrench", "0");
}

function check_translation() {
  const isFrench = localStorage.getItem("isFrench");
  return parseInt(isFrench);
}

function translate() {
  if (check_translation() === 1) {
    translate_fr();
    return;
  }

  translate_eng();
}

translate();
  • 在加载时触发函数 translate()。
  • 这可以检查本地存储值localStorage.getItem("isFrench");。
  • 本地存储是字符串,因此转换为 int 但不是真正需要这样做。
  • 当用户执行您的 onClick 时会设置哪个 事件(不在本例中)
  • 如果为 1,则显示法语。
  • 如果为 0,则显示英文。

【讨论】:

  • 请在发布答案之前检查重复项。这不是重复的可能性为零(这个问题也与翻译本身无关;暗模式解决方案在这里也是重复的)
  • 绝对是重复的!
  • 是的,那你为什么要发布答案?
  • 有一段时间了,想帮助 OP :)
  • 当然可以,但是如果您链接到合适的骗子,您将帮助他们、其他用户和模组;)
猜你喜欢
  • 1970-01-01
  • 2014-02-12
  • 1970-01-01
  • 2021-04-02
  • 1970-01-01
  • 2011-11-03
  • 2015-05-16
  • 2020-08-31
  • 2021-06-24
相关资源
最近更新 更多