Heim  >  Fragen und Antworten  >  Hauptteil

javascript - Wie kann der Fehler behoben werden? Die Eigenschaft „Wert“ kann beim Ausführen einer JS-Funktion nicht auf Null gesetzt werden?

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>计时器</title>
<script type="text/javascript">
  var num=0;
  function startCount() {
    document.getElementById('count').value=num;
    num=num+1;
    setTimeout(startCount(),1000);
  }
  startCount();
</script>
</head>
<body>
<form>
<input type="text" id="count" />
</form>
</body>
</html>

Warum kann ich startCount() nicht direkt in Zeile 13 schreiben? Die Fehlermeldung Uncaught TypeError: Cannot set property 'value' of null。我在html标签下方写一个script标签,里面写startCount()又可以了,或者13行写成setTimeout(startCount(),1000); kann auch ausgeführt werden.

phpcn_u1582phpcn_u15822687 Tage vor1422

Antworte allen(1)Ich werde antworten

  • 代言

    代言2017-06-12 09:27:38

    dom还没有完全加载完成原因,所以js建议放在body下面执行,或者用window.onload就能放在head头部执行。

    Antwort
    0
  • StornierenAntwort