如何在点击按钮后再次执行脚本

huangapple go评论59阅读模式
英文:

How to execute a script again after click button

问题

I have a HTML template with an external script to manage cookies permissions:

<!DOCTYPE html>
<html lang="fr">
  <head>
    <script id="consentementCookiesScript"  src="https://static.desjardins.com/fw/cookie/scriptCookie.js"></script>

The cookies permissions' language is set according to the Lang attribute in the HTML tag.

I manage creating a button to dynamically change the Lang attribute:

<div id="toolbar" class="toolbar">
  <div id="changeLangElement" style="color: white">Français</div>
</div>

through this function:

changeLangElement.onclick = function () {
  currentLang = currentLang === "fr" ? "en" : "fr";
  translatePage(currentLang);
  document.documentElement.lang = currentLang;
};

Is there any way to execute the script again with the right language after clicking the button?

英文:

I have a HTML template with an external script to manage cookies permissions:

&lt;!DOCTYPE html&gt;
  &lt;html lang=&quot;fr&quot;&gt;
    &lt;head&gt;
      &lt;script id=&quot;consentementCookiesScript&quot;  src=&quot;https://static.desjardins.com/fw/cookie/scriptCookie.js&quot; &gt;&lt;/script&gt;

The cookies permissions' language is set according with the Lang att into the tag html.<br>
I manage creating a btn to change dynamically the Lang att:

&lt;div id=&quot;toolbar&quot; class=&quot;toolbar&quot;&gt;
  &lt;div id=&quot;changeLangElement&quot; style=&quot;color: white&quot;&gt;Fran&#231;ais&lt;/div&gt;
&lt;/div&gt;

through this function:

changeLangElement.onclick = function () {
  currentLang = currentLang === &quot;fr&quot; ? &quot;en&quot; : &quot;fr&quot;;
  translatePage(currentLang);
  document.documentElement.lang = currentLang;
};

Is there any way to execute the script again with the right language after click the button?

答案1

得分: 1

我认为您需要为该按钮或div添加一个事件监听器,就像这样:

const changeLangElement = document.querySelector('#changeLangElement');

changeLangElement.addEventListener('click', () => {
  currentLang = currentLang === "fr" ? "en" : "fr";
  translatePage(currentLang);
  document.documentElement.lang = currentLang;
});
英文:

I think you have to add an Event listener attached to that button or div like this :

const changeLangElement = document.querySelector(&#39;#changeLangElement&#39;) ; 

changeLangElement.addEventListener(&#39;click&#39; , () =&gt; {
  currentLang = currentLang === &quot;fr&quot; ? &quot;en&quot; : &quot;fr&quot;;
  translatePage(currentLang);
  document.documentElement.lang = currentLang;
}) ;

答案2

得分: 1

您可以将`currentLang`变量移到函数范围之外或者像下面这样使用**闭包**

```js

function createLangToggleHandler() {
  let currentLang = "en";

  return function () {
    currentLang = currentLang === "fr" ? "en" : "fr";
    translatePage(currentLang);
    document.documentElement.lang = currentLang;
  };
}

const changeLangElement = document.getElementById("changeLang");
changeLangElement.onclick = createLangToggleHandler();
英文:

You can either move the currentLang variable outside of the function scope or use closure like so:


function createLangToggleHandler() {
  let currentLang = &quot;en&quot;;

  return function () {
    currentLang = currentLang === &quot;fr&quot; ? &quot;en&quot; : &quot;fr&quot;;
    translatePage(currentLang);
    document.documentElement.lang = currentLang;
  };
}

const changeLangElement = document.getElementById(&quot;changeLang&quot;);
changeLangElement.onclick = createLangToggleHandler();

huangapple
  • 本文由 发表于 2023年5月30日 01:57:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359438.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定