javascript - 单击 EN 更改为 AR,反之亦然
问题描述
当我单击AR
按钮时,它会变为EN
按钮。然后,当我单击EN
按钮时,它不会返回AR
按钮。
这是我的代码:
function change() {
if (document.getElementById('AE').value = "AR") {
document.getElementById('AE').innerHTML = "EN";
document.getElementById('body').dir = "rtl";
fetch("https://api.npoint.io/abb3ff619eec383b1bb7")
.then(response => response.json())
.then(data => {
document.querySelector("#lang").innerText = data.name[0].ara;
})
} else {
document.getElementById('AE').innerHTML = "AR";
document.getElementById('body').dir = "ltr";
fetch("https://api.npoint.io/abb3ff619eec383b1bb7")
.then(response => response.json())
.then(data => {
document.querySelector("#lang").innerText = data.name[0].eng;
})
};
}
<body id="body">
<button style="color:black;" id="AE" onclick="change()">AR</button>
<div id="lang">Amjad</div>
</body>
当我单击EN
更改为时,我想要AR
,反之亦然。如何解决这个问题?
解决方案
首先使用innerText
而value
不是button
. 然后注意if
您使用的 in 子句=
而不是==
.
这是工作片段:
function change() {
if (document.getElementById('AE').innerText == "AR") {
document.getElementById('AE').innerHTML = "EN";
document.getElementById('body').dir = "rtl";
fetch("https://api.npoint.io/abb3ff619eec383b1bb7")
.then(response => response.json())
.then(data => {
document.querySelector("#lang").innerText = data.name[0].ara;
})
} else {
document.getElementById('AE').innerHTML = "AR";
document.getElementById('body').dir = "ltr";
fetch("https://api.npoint.io/abb3ff619eec383b1bb7")
.then(response => response.json())
.then(data => {
document.querySelector("#lang").innerText = data.name[0].eng;
})
};
}
<body id="body">
<button style="color:black;" id="AE"onclick="change()">AR</button>
<div id="lang">Amjad</div>
</body>
推荐阅读
- google-apps-script - 当函数仅限于特定列时,为什么撤消会触发我的 onEdit()?
- reporting-services - SSRS 在文本框表达式中重复文本
- javascript - React.JS 新的 CSS 模块
- regex - 如何将解析模板写到行尾?
- javascript - 失踪 ; 通过从jsp中的数据库获取数据在javascript中创建对象时出现之前的语句错误
- html - 如何选择循环中的 div 内的最后两个孩子而不给该选定的孩子一个边框
- ruby - 使用模的偶数或奇数程序
- java - Storm Kafka Spout 的延迟非常高
- php - 如何在wordpress中删除菜单标签
- database - AWS 将数据从一个数据库复制到另一个