suchen

Heim  >  Fragen und Antworten  >  Hauptteil

TypeError: Die Eigenschaft „textContent“ kann mit textContent nicht auf null gesetzt werden

let firstCard = 10;
let secondCard = 4;
let sum = firstCard + secondCard;
let hasBlackJack = false;
let isAlive = true;
let message = ""
let messageEl = document.getElementById("message-el");
let sumEl = document.querySelector("#sum-el");
let cardsEl = document.getElementById("cards-el");
messageEl.textContent = "Do you want to draw a new card?";

function startGame() {
    cardsEl.textContent = "Cards : " + firstCard + " " + secondCard;
    sumEl.textContent = "Sum :" + sum;
    if (sum <= 20) {
        message = "Do you want to draw a new card?";
    } else if (sum === 21) {
        message = "You have got blackjack!";
        hasBlackJack = ture;
    } else {
        message = "You are out of the game!";
        isAlive = false;
    }
    messageEl.textContent = message;
}

P粉310754094P粉310754094452 Tage vor507

Antworte allen(1)Ich werde antworten

  • P粉969666670

    P粉9696666702023-09-08 16:32:36

    这一行:

    let messageEl = document.getElementById("message-el");

    没有返回元素。

    这是因为在代码执行时,该元素在 DOM 中不存在。这可能是因为您没有任何具有该 ID 的元素,或者因为您需要在将该元素解析到 DOM 后移动代码来执行。

    Antwort
    0
  • StornierenAntwort