Javascript - div innerHTML不会改变

时间:2017-11-07 07:10:35

标签: javascript function dom switch-statement innerhtml



var score = 0, time = 1, heart = 3;

window.onload = function() {

  var input = document.getElementById("wordTyped");
  var timeLeft = document.getElementById("time");
  var life = document.getElementById("life");

  input.addEventListener("click", timer, false);

  function timer() {
    var id = setInterval(countdown, 10);

    function countdown() {
      input.removeEventListener("click", timer, false);
      timeLeft.innerHTML = "Time left: " + (time - 0.01).toFixed(2) + "s";
      time = 1 * (time - 0.01).toFixed(2);

      if (time == 0 && life.innerHTML == "") {
        clearInterval(id);
      } else if (time == 0) {
        --heart;
        time = 1;
        life(heart);
      }
    }

    function life(heart) {

      heart *= 1; // To make sure it is a number type
      console.log(heart);

      switch (heart) {
        case 2:
          life.innerHTML = "❤️❤️";
          console.log(life.innerHTML);
          break;
        case 1:
          life.innerHTML = "❤️";
          console.log(life.innerHTML);
          break;
        case 0:
        default:
          life.innerHTML = "";
          console.log(life.innerHTML);
          break;
      }
      /*if(heart === 2) {
        life.innerHTML = "❤️❤️";
      }
      else if(heart == 1) {
        life.innerHTML = "❤️";
      }
      else {
        life.innerHTML = "";
      }*/
    }
  }

}

<div id="wordGenerated">illustration</div>

<input id="wordTyped" type="text" />

<div id="time">Time left: 1.00s</div>

<div id="score">Score: 0</div>

<div id="life">❤️❤️❤️</div>
&#13;
&#13;
&#13;

我不确定function life(heart)中的错误。

我试图减少&#39;❤️&#39;当一个时间点击0并重置回其原始值时重复一次,重复直到heart等于0。

如果我在life.innerHTML = " example "范围之外使用function timer(),则会有效。

使用console.log(),表明life.innerHTML 已更改,但HTML文档的显示保持不变,我不明白为什么。

我已经尝试了.nodeValue.innerText.textContent,但所有人仍然提供了相同的结果

2 个答案:

答案 0 :(得分:2)

life变量和life()函数存在冲突使用。将life()函数名称更改为其他内容,例如updateLife(),您的代码可以正常运行,

注意:范围内的变量或函数或对象不能具有相同的名称。

<强>演示

var score = 0, time = 1, heart = 3;

window.onload = function() {

  var input = document.getElementById("wordTyped");
  var timeLeft = document.getElementById("time");
  var life = document.getElementById("life");

  input.addEventListener("click", timer, false);

  function timer() {
    var id = setInterval(countdown, 10);

    function countdown() {
      input.removeEventListener("click", timer, false);
      timeLeft.innerHTML = "Time left: " + (time - 0.01).toFixed(2) + "s";
      time = 1 * (time - 0.01).toFixed(2);

      if (time == 0 && life.innerHTML == "") {
        clearInterval(id);
      } else if (time == 0) {
        --heart;
        time = 1;
        updateLife(heart);
      }
    }

    function updateLife(heart) {

      heart *= 1; // To make sure it is a number type
      console.log(heart);

      switch (heart) {
        case 2:
          life.innerHTML = "❤️❤️";
          console.log(life.innerHTML);
          break;
        case 1:
          life.innerHTML = "❤️";
          console.log(life.innerHTML);
          break;
        case 0:
        default:
          life.innerHTML = "";
          console.log(life.innerHTML);
          break;
      }
      /*if(heart === 2) {
        life.innerHTML = "❤️❤️";
      }
      else if(heart == 1) {
        life.innerHTML = "❤️";
      }
      else {
        life.innerHTML = "";
      }*/
    }
  }

}
<div id="wordGenerated">illustration</div>

<input id="wordTyped" type="text" />

<div id="time">Time left: 1.00s</div>

<div id="score">Score: 0</div>

<div id="life">❤️❤️❤️</div>

答案 1 :(得分:1)

生活和生活功能之间存在冲突,所以你可以像$(“#life”)。innerHTML =“”或其他任何东西