JQuery点击功能只有在首次双击后才会触发。

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

JQuery click function doesn't trigger unless double clicked first

问题

我有一个具有类名为 language-switch 的 div,位于导航栏中,作为更改语言的按钮。我编写了一个 jQuery 代码来更改 Main id 内的类和文本,但它只能在第二次初始点击后触发。

我已经尝试将其包装在 $(document).ready(function(){} 中,但没有成功。

令人困惑的是,我还有一个用于从浅色主题更改为深色主题的按钮,名为 mode-switch,它在第一次点击时就可以工作,而无需双击。

以下是其 HTML 代码:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<div class="navbar-wrapper">
    <div class="topnav" id="myTopnav">
        <span class="menu">
            <div class="language-switch">
                <div class="language">EN </div>
                <i class="fa fa-globe"></i>
            </div>
            <div class="mode-switch">
                <i class="fa fa-sun-o"></i>
            </div>
        </span>
    </div>
</div>

language-switch 按钮的 JavaScript 代码:

$(document).ready(function(){
    $(".language-switch").click(function(){
        if ($(this).children(".language").text() == "EN") {
            $(this).children(".language").text("ID");
            $("#main").attr("class", "lang-id");
        }
        else {
            $(this).children(".language").text("EN");  
            $("#main").attr("class", "lang-en");      
        }
        console.log($(this).children(".language").text());
        console.log($("#main").attr("class"));
    });   
});

mode-switch 按钮的 JavaScript 代码:

$(document).ready(function(){
    $(".mode-switch").click(function(){
        $("i", this).toggleClass("fa-moon-o fa-sun-o");
        $(".light-theme").toggleClass("dark-theme");
        console.log($(this).attr("class"));
    });   
});
英文:

I have a div with class language-switch in a navigation bar that acts as a button to change the language. I wrote a JQuery code to change the class of the Main id and text inside the div, but it can only trigger after the second initial clicks.

I already tried wrapping it in $(document).ready(function(){} to no avail.

Confusingly I also have a button to change the theme from light to dark called mode-switch and it works the first time without having to be double clicked first.

Here's the html for it.

&lt;link rel=&quot;stylesheet&quot; href=&quot;https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css&quot;&gt;
&lt;div class=&quot;navbar-wrapper&quot;&gt;
        &lt;div class=&quot;topnav&quot; id=&quot;myTopnav&quot;&gt;
          &lt;span class=&quot;menu&quot;&gt;
            &lt;div class=&quot;language-switch&quot;&gt;
                &lt;div class=&quot;language&quot;&gt;EN &lt;/div&gt;
                &lt;i class=&quot;fa fa-globe&quot;&gt;&lt;/i&gt;
            &lt;/div&gt;
            &lt;div class=&quot;mode-switch&quot;&gt;
                &lt;i class=&quot;fa fa-sun-o&quot;&gt;&lt;/i&gt;
            &lt;/div&gt;
          &lt;/span&gt;
        &lt;/div&gt;
      &lt;/div&gt;

JavaScript for the language-switch button.

$(document).ready(function(){
    $(function changeLanguage(){
        $(&quot;.language-switch&quot;).click(function(){
        if ($(this).children(&quot;.language&quot;).text() == &quot;EN&quot;) {
            $(this).children(&quot;.language&quot;).text(&quot;ID&quot;);
            $(&quot;#main&quot;).attr(&quot;class&quot;, &quot;lang-id&quot;);
        }
        else {
            $(this).children(&quot;.language&quot;).text(&quot;EN&quot;);  
            $(&quot;#main&quot;).attr(&quot;class&quot;, &quot;lang-en&quot;);      
        }
        console.log($(this).children(&quot;.language&quot;).text());
        console.log($(&quot;#main&quot;).attr(&quot;class&quot;));
        });   
    });
});

and the JavaScript for mode-switch button.

$(document).ready(function(){
    $(&quot;.mode-switch&quot;).click(function(){
      $(&quot;i&quot;, this).toggleClass(&quot;fa-moon-o fa-sun-o&quot;);
      $(&quot;.light-theme&quot;).toggleClass(&quot;dark-theme&quot;);
      console.log($(this).attr(&quot;class&quot;));
    });   
});

答案1

得分: 0

以下是您要翻译的内容:

你有一个名为 changeLanguage 的函数,该函数在 $(".language-switch") 元素上创建了一个事件监听器。在提供的代码中,似乎 changeLanguage 函数实际上从未被调用。

另外,语言节点末尾有一个空格,所以最初 "EN " 不等于 "EN"。

只需使用:

$(document).ready(function(){
    $(".language-switch").click(function(){
        if ($(this).children(".language").text().trim() == "EN") {
            $(this).children(".language").text("ID");
            $("#main").attr("class", "lang-id");
        }
        else {
            $(this).children(".language").text("EN");  
            $("#main").attr("class", "lang-en");      
        }
        console.log($(this).children(".language").text());
        console.log($("#main").attr("class"));
    });   
});

另外考虑使用 classAddclassRemoveclassToggle,而不是完全覆盖类属性:

$(document).ready(function(){
    $(".language-switch").click(function(){
        var isEn = $(this).children(".language").text("ID").trim() == "EN";

        $(this).children(".language").text(isEn ? "ID" : "EN");
        $("#main").toggleClass("lang-id", isEn)
                  .toggleClass("lang-en", !isEn);

        console.log($(this).children(".language").text());
        console.log($("#main").attr("class"));
    });   
});
英文:

You have a function changeLanguage which in turn creates an event listener on $(&quot;.language-switch&quot;) elements. It appears the changeLanguage function is never actually called in the code provided.

Additionally you have a trailing space in the language node so, initially "EN " != "EN"

Just use:

$(document).ready(function(){
    /*$(function changeLanguage(){ -- remove the function*/
        $(&quot;.language-switch&quot;).click(function(){
        if ($(this).children(&quot;.language&quot;).text().trim() == &quot;EN&quot;) {
            $(this).children(&quot;.language&quot;).text(&quot;ID&quot;);
            $(&quot;#main&quot;).attr(&quot;class&quot;, &quot;lang-id&quot;);
        }
        else {
            $(this).children(&quot;.language&quot;).text(&quot;EN&quot;);  
            $(&quot;#main&quot;).attr(&quot;class&quot;, &quot;lang-en&quot;);      
        }
        console.log($(this).children(&quot;.language&quot;).text());
        console.log($(&quot;#main&quot;).attr(&quot;class&quot;));
        });   
   /* }); */
});

Also consider using classAdd. classRemove or classToggle instead of overriding the class attribute entirely

$(document).ready(function(){
        $(&quot;.language-switch&quot;).click(function(){
        var isEn = $(this).children(&quot;.language&quot;).text(&quot;ID&quot;).trim() == &quot;EN&quot;;

        $(this).children(&quot;.language&quot;).text(isEn ? &quot;ID&quot; : &quot;EN&quot;);
        $(&quot;#main&quot;).togglClass(&quot;lang-id&quot;, isEn)
                  .togglClass(&quot;lang-en&quot;, !isEn);

        console.log($(this).children(&quot;.language&quot;).text());
        console.log($(&quot;#main&quot;).attr(&quot;class&quot;));
        });   
});

huangapple
  • 本文由 发表于 2023年7月17日 12:00:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701409.html
匿名

发表评论

匿名网友

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

确定