一个按钮表明浏览器是错误的,即使不是

时间:2019-04-30 01:16:58

标签: javascript html code.org

当我突然在Code.org网络实验室中创建的按钮上测试了一个新的里程碑时,突然开始说我使用的浏览器不支持该代码。

我尝试删除添加的新代码,没有任何反应。我尝试修复代码错误,没有任何反应。我什至尝试通过复制和粘贴上一个问题中的代码来还原代码的先前格式,因此未发生任何事情。

这是当前的javascript代码,包括新的里程碑:

<script>
      function clickCounter() {
        if (typeof(Storage) !== "undefined") {
          if (localStorage.clickcount) {
            localStorage.clickcount = Number(localStorage.clickcount)+1;
          } else {
            localStorage.clickcount = 1;
          }
          document.getElementById("result").innerHTML = "People have attempted but failed to help me <span id='recolor'>" + localStorage.clickcount + "</span> times.";

          if (localStorage.clickcount >= 100) {
            document.getElementById("recolor").style.color = "#bc5c18";
          }
          if (localStorage.clickcount >= 250) {
            document.getElementById("recolor").style.color = "#c9c9c9";
          }
          if (localStorage.clickcount >= 500) {
            document.getElementById("recolor").style.color = "#e5d860";
          }
          if (localStorage.clickcount >= 1000) {
            document.getElementById("recolor").style.color = "#6af2eb";
          }
          if (localStorage.clickcount >= 5000) {
            document.getElementById("recolor").style.color = "#42f456";
          }
          else {
          document.getElementById("result").innerHTML = "Sorry, your browser does not support helping me.";
          }
        }
      }
    </script>

只有当我添加了最后一个时,它才停止工作。

以下是增加计数器的按钮代码:

<p><button onclick="clickCounter()" type="button"><b>HELP ME</b></button></p>
    <div id="result"></div>

我希望它能输出以下结果:

People have attempted and failed to help me x times

但是它给出的实际输出是:

Sorry, your browser doesn't support helping me.

无论我尝试什么,此结果都不会改变。我该怎么办?

1 个答案:

答案 0 :(得分:0)

您需要拥有else if

          if (localStorage.clickcount <= 100) {
            document.getElementById("recolor").style.color = "#bc5c18";
          }
          else if (localStorage.clickcount <= 250) {
            document.getElementById("recolor").style.color = "#c9c9c9";
          }
          else if (localStorage.clickcount <= 500) {
            document.getElementById("recolor").style.color = "#e5d860";
          }
          else if (localStorage.clickcount <= 1000) {
            document.getElementById("recolor").style.color = "#6af2eb";
          }
          else if (localStorage.clickcount <= 5000) {
            document.getElementById("recolor").style.color = "#42f456";
          }
          else {
          document.getElementById("result").innerHTML = "Sorry, your browser does not support helping me.";
          }