TypeError: Die Eigenschaft „textContent' kann mit textContent nicht auf null gesetzt werden
P粉310754094
P粉310754094 2023-09-07 23:10:11
0
1
509

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粉310754094
P粉310754094

Antworte allen(1)
P粉969666670

这一行:

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

没有返回元素。

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

Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage