英文:
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.
<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>
JavaScript for the language-switch
button.
$(document).ready(function(){
$(function changeLanguage(){
$(".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"));
});
});
});
and the JavaScript for mode-switch
button.
$(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"));
});
});
答案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"));
});
});
另外考虑使用 classAdd
、classRemove
或 classToggle
,而不是完全覆盖类属性:
$(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 $(".language-switch")
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*/
$(".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"));
});
/* }); */
});
Also consider using classAdd
. classRemove
or classToggle
instead of overriding the class attribute entirely
$(document).ready(function(){
$(".language-switch").click(function(){
var isEn = $(this).children(".language").text("ID").trim() == "EN";
$(this).children(".language").text(isEn ? "ID" : "EN");
$("#main").togglClass("lang-id", isEn)
.togglClass("lang-en", !isEn);
console.log($(this).children(".language").text());
console.log($("#main").attr("class"));
});
});
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论