我正在:未捕获的TypeError:无法将属性'textContent'设置为null

时间:2020-07-25 05:35:28

标签: javascript html dom

我正在使用Visual Studio Code。我试图将textContent属性设置为等于var dice,但显示错误。 这是我的屏幕截图,请帮助我。 另外,当我尝试设置addEventListener属性时,也会发生相同的错误。 我的HTML代码-

<html>
    <head>
        <meta charset="utf-8">
        <title>DICE GAME</title>
        <link rel="stylesheet" href="assets\stylesheet\main.css">
        <link href="https://fonts.googleapis.com/css2?family=Kaushan+Script&display=swap" rel="stylesheet">
        <script src="script.js"></script>
    </head>
<body class="container">
<section class="wrapper">
        <button class="btn-new" id="newgame">NEW GAME</button>
        <img src="dice-1.png" height="60px" width="60px" class="image" id="dice">
    <section class="col-1-2">
        <h1 id="player-0">PLAYER 1</h1>
        <h2 id="roundscore-0">40</h2>
    <div class="currentscore">
        <h3>Current Score</h3>
        <h4 id="Current-0">5</h4>
    </div>
    </section>
    <section class="col-1-2">
        <h1 id="player-1">PLAYER 2</h1>
        <h2 id="roundscore-1">54</h2>
    <div class="currentscore">
        <h3>Current Score</h3>
        <h4 id="Current-1">2</h4>
    </div>
    </section>
    <section>
            <button class="btn-roll">ROLL DICE</button>
            <button class="btn-hold">HOLD</button>
    </section>
</section>
</body>
</html>

我的JavaScript代码-

scores = [0,0];
roundscore = 0;
activeplayer = 0;

dice = Math.floor(Math.random() * 6) + 1;

document.getElementById('Current-1').textContent = dice;```

1 个答案:

答案 0 :(得分:0)

将此标签放在身体底部。

<script src="script.js"></script>

<html>
    <head>
        <meta charset="utf-8">
        <title>DICE GAME</title>
        <link rel="stylesheet" href="assets\stylesheet\main.css">
        <link href="https://fonts.googleapis.com/css2?family=Kaushan+Script&display=swap" rel="stylesheet">
    </head>
<body class="container">
<section class="wrapper">
        <button class="btn-new" id="newgame">NEW GAME</button>
        <img src="dice-1.png" height="60px" width="60px" class="image" id="dice">
    <section class="col-1-2">
        <h1 id="player-0">PLAYER 1</h1>
        <h2 id="roundscore-0">40</h2>
    <div class="currentscore">
        <h3>Current Score</h3>
        <h4 id="Current-0">5</h4>
    </div>
    </section>
    <section class="col-1-2">
        <h1 id="player-1">PLAYER 2</h1>
        <h2 id="roundscore-1">54</h2>
    <div class="currentscore">
        <h3>Current Score</h3>
        <h4 id="Current-1">2</h4>
    </div>
    </section>
    <section>
            <button class="btn-roll">ROLL DICE</button>
            <button class="btn-hold">HOLD</button>
    </section>
</section>
<script src="script.js"></script>

</body>
</html>