Jump to content
  • 0

Не могу решить проблему TypeError: Cannot set property 'innerHTML' of null


Crazy54789
 Share

Question

function showModal(id) {

    document.getElementsByClassName(`modal-1`)[0].classList.add('active')
}

function closeGame(id) {

    document.getElementsByClassName(`modal-1`)[0].classList.remove('active')
}

function getRandomImage() {
	var imgArray =
		['<img src="img/0.jpg" alt="#" />',
		'<img src="img/1.jpg" alt="#" />',
		'<img src="img/2.jpg" alt="#" />',
		'<img src="img/3.jpg" alt="#" />',
		'<img src="img/4.jpg" alt="#" />',
		'<img src="img/5.jpg" alt="#" />',
		'<img src="img/6.jpg" alt="#" />',
		'<img src="img/7.jpg" alt="#" />',
		'<img src="img/8.jpg" alt="#" />',
		'<img src="img/9.jpg" alt="#" />',
		'<img src="img/10.jpg" alt="#" />'];
	var rand = Math.floor(Math.random() * imgArray.length);
	var randImg = imgArray[rand];
	var imgTag = document.getElementById('img');
	imgTag.innerHTML = randImg;
}
window.onload = getRandomImage;

Вот здесь выдает ошибку в строке imgTag.innnerHTML = randImg;

HTML код

 

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Lr_8</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>

    <input class="player-name" value ="Print your nickname..."></input>
    <button class="play" onclick="showModal(1)">Play</button>

    <div class="modal modal-1">
        <div class="modal-window">

            <div class="modal-header">
                <h4 class="title">Lr_8</h4>
                <div class="close" onclick="closeGame(1)">&times;</div>
            </div>

            <div class="modal-body">

                <div class="img_">
                    <div class="img img-player"></div>
                    <div class="img img-enemy"></div>
                </div>

                <div class="nickname">
                    <output class="player"></output>
                    <output class="enemy"></output>
                </div>

                <div class="point_">
                    <div class="point point-player"></div>
                    <div class="point point-enemy"></div>
                </div>

                <div class="number_">
                    <div class="number number-player"></div>
                    <div class="number number-enemy"></div>
                </div>

                <div class="number_">
                    <button class="generate" onclick="generateNum()"></button>
                </div>

            </div>

                </div>
            </div>
        </div>
    </div>
</body>
<script src="script.js"></script>
</html>

Я студент, только учусь вебу возможно код кривой ехех

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue. See more about our Guidelines and Privacy Policy