js文件中调用了html中的某个节点,并且js在这个调用的对象之前加载了。
如果js放在头部时,此时浏览器渲染未进入dom节点,因为提示为空,或者找不到。
所以把js代码放到最后或调用的对象后面就行了。
错误代码:
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Today's Date</title>
<script type ="text/javascript">
let d = new Date();
document.body.innerHTML = "<h1>Today's date is " + d + "</h1>"
</script>
</head>
<body>
</body>
</html>
修改:
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Today's Date</title>
</head>
<body>
<script type ="text/javascript">
let d = new Date();
document.body.innerHTML = "<h1>Today's date is " + d + "</h1>"
</script>
</body>
</html>