0

我正在关注一个使用 js 文件使静态页面动态化的教程,并且我逐字输入了所有内容,但是当我用浏览器打开它时,我的页面保持静态。我在同一目录中有 HTML 文件和 js 文件。

JS 和 HTML 文件

//some great message options
const vibes = [
    "... and you are awesome!",
    "... have an amazing day!",
    "... love to see you win!",
    "... can't wait to see what you accomplish today!",
    "... You got this!"
];

var vibe = vibes[Math.floor(Math.random() * Math.floor(vibes.length))];

// diplay a good vibe
document.querySelector(".vibe").append(vibe);
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>A page for you</title>
    <style>
        body{
            text-align:center;
        }
        div.container{
            width: 400px;
            margin: 0 auto;
        }
        .love{
            font-size: 4em;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>This is a page for you</h1>
        </div class="vibe">You are great!</div>
        <div class="love">
            ❤️
        </div>
    </div>
    <script type="text/javascript" src="vibe.js"></script>
</body>
</html>

如何获取 html 文件来选择 js 脚本并随机化消息?

4

1 回答 1

0

解决这个问题;

 </div class="vibe">You are great!</div>

它应该是

<div class="vibe">You are great!</div>
于 2021-12-04T20:28:53.513 回答