首页 > 解决方案 > 单击 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,反之亦然。如何解决这个问题?

标签: javascripthtml

解决方案


首先使用innerTextvalue不是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>

   


推荐阅读