innerHTML不会将文本添加到div

时间:2018-12-02 04:12:48

标签: javascript file

<html>
<body>
<div class = "container">
    <audio id="music" src="/music/orgmusic.mp3"type="audio/mpeg"></audio>
    <img id="titleimg"src = "images/titleimage.png">
        <div class = "gameContainer">
        </div>
        <div id ="play">Press Spacebar To Return To Main Menu!</div>
</div>
<link rel="stylesheet" type="text/css" href="/css/setup.css">
<link rel="stylesheet" type="text/css" href="/css/global.css">

<script src="/JS/setup.js" type="text/javascript"></script>
<script src="/JS/global.js" type="text/javascript"></script>
</body>
</html>

JS文件:

document.getElementById("gameContainer").innerHTML = "hi this is words";

如您所见,我设置了将脚本插入到我的div中的名为“ gameContainer”的嵌入式脚本,但是由于某些原因它无法正常工作。

我知道我可以键入它,但是我不想让它通过我的JS文件工作。 为什么不插入文本有什么想法?

2 个答案:

答案 0 :(得分:1)

您正在基于Id做事,并在HTML中设置了类。

<div class = "gameContainer"> // Hear you used class
</div>

在Js中,您使用了document.getElementById("gameContainer").innerHTML

<div id="gameContainer"> // Hear you have to used id instead of class
</div>

答案 1 :(得分:-1)

document.getElementById("gameContainer").innerHTML = "hi this is words";
<html>
<body>
<div class = "container">
    <audio id="music" src="/music/orgmusic.mp3"type="audio/mpeg"></audio>
    <img id="titleimg"src = "images/titleimage.png">
        <div id = "gameContainer"class = "gameContainer">
        </div>
        
        <div id ="play">Press Spacebar To Return To Main Menu!</div>
</div>
<link rel="stylesheet" type="text/css" href="/css/setup.css">
<link rel="stylesheet" type="text/css" href="/css/global.css">

<script src="/JS/setup.js" type="text/javascript"></script>
<script src="/JS/global.js" type="text/javascript"></script>
</body>
</html>