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

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

How to execute a script again after click button

问题

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

  1. <!DOCTYPE html>
  2. <html lang="fr">
  3. <head>
  4. <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:

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

through this function:

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

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:

  1. &lt;!DOCTYPE html&gt;
  2. &lt;html lang=&quot;fr&quot;&gt;
  3. &lt;head&gt;
  4. &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:

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

through this function:

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

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

答案1

得分: 1

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

  1. const changeLangElement = document.querySelector('#changeLangElement');
  2. changeLangElement.addEventListener('click', () => {
  3. currentLang = currentLang === "fr" ? "en" : "fr";
  4. translatePage(currentLang);
  5. document.documentElement.lang = currentLang;
  6. });
英文:

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

  1. const changeLangElement = document.querySelector(&#39;#changeLangElement&#39;) ;
  2. changeLangElement.addEventListener(&#39;click&#39; , () =&gt; {
  3. currentLang = currentLang === &quot;fr&quot; ? &quot;en&quot; : &quot;fr&quot;;
  4. translatePage(currentLang);
  5. document.documentElement.lang = currentLang;
  6. }) ;

答案2

得分: 1

  1. 您可以将`currentLang`变量移到函数范围之外或者像下面这样使用**闭包**
  2. ```js
  3. function createLangToggleHandler() {
  4. let currentLang = "en";
  5. return function () {
  6. currentLang = currentLang === "fr" ? "en" : "fr";
  7. translatePage(currentLang);
  8. document.documentElement.lang = currentLang;
  9. };
  10. }
  11. const changeLangElement = document.getElementById("changeLang");
  12. changeLangElement.onclick = createLangToggleHandler();
英文:

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

  1. function createLangToggleHandler() {
  2. let currentLang = &quot;en&quot;;
  3. return function () {
  4. currentLang = currentLang === &quot;fr&quot; ? &quot;en&quot; : &quot;fr&quot;;
  5. translatePage(currentLang);
  6. document.documentElement.lang = currentLang;
  7. };
  8. }
  9. const changeLangElement = document.getElementById(&quot;changeLang&quot;);
  10. 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:

确定