我刚刚开始学习使用html、css和JavaScript进行web开发。但将我的文件与index.js文件连接后,页面上没有显示任何内容。首先它发出警报(“Hello from JavaScript”),然后直到我关闭警报弹出窗口,页面内容才显示出html文件的代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="index.js"></script>
</head>
<body>
<h1>First Website </h1>
<p>Lorem ipsum dolor, sit amet consectetur adipisicing elit. Repellat similique vitae, aliquam in ipsum explicabo saepe repudiandae quidem recusandae vel dolores beatae iure temporibus aliquid libero adipisci alias obcaecati magni ratione nesciunt impedit mollitia. Tempore nulla porro, deleniti architecto fugit animi ut, inventore asperiores nobis labore cumque soluta. Suscipit, laborum.</p>
</body>
</html>
可能的原因是您的script
标记,因为您的script
标记放置在正文之前。放置所有script
标记的更好位置是body
标记的底部。
<head>
<title>Document</title>
</head>
<body>
<!-- script tags at the bottom of body -->
<script src="index.js"></script>
</body>
</html>