Javascript Three.js error document.getElementById
我一直在四处寻找,但找不到有同样问题的人。我刚开始学习使用Three.js,本(Lynda.com)教程指导我编写以下php文件和javascript代码Javascript Three.js error document.getElementById,javascript,html,three.js,Javascript,Html,Three.js,我一直在四处寻找,但找不到有同样问题的人。我刚开始学习使用Three.js,本(Lynda.com)教程指导我编写以下php文件和javascript代码 <?php echo "Three JS Course"; ?> <!DOCTYPE html> <html> <head> <title>Three.js Course</title> </head> <body> <
<?php
echo "Three JS Course";
?>
<!DOCTYPE html>
<html>
<head>
<title>Three.js Course</title>
</head>
<body>
<div id="webgl"></div>
</body>
chrome控制台显示以下错误:
main.js:25 Uncaught TypeError: Cannot read property 'appendChild' of null
at init (main.js:25)
at main.js:36
<?php
echo "Three JS Course";
?>
<!DOCTYPE html>
<html>
<head>
<title>Three.js Course</title>
</head>
<body>
<div id="webgl"></div>
</body>
我不太明白我做错了什么,我创建了一个具有相同id的div元素
<?php
echo "Three JS Course";
?>
<!DOCTYPE html>
<html>
<head>
<title>Three.js Course</title>
</head>
<body>
<div id="webgl"></div>
</body>
PHP:
<?php
echo "Three JS Course";
?>
<!DOCTYPE html>
<html>
<head>
<title>Three.js Course</title>
</head>
<body>
<div id="webgl"></div>
</body>
我找到了解决方案,因此我将发布一个答案,以防其他人遇到此问题:
<?php
echo "Three JS Course";
?>
<!DOCTYPE html>
<html>
<head>
<title>Three.js Course</title>
</head>
<body>
<div id="webgl"></div>
</body>
确保在HTML文件之后加载脚本。如果document.getElementById()尝试访问尚未加载的元素,它将无法找到它 JS在文件中的位置?可能是
<?php
echo "Three JS Course";
?>
<!DOCTYPE html>
<html>
<head>
<title>Three.js Course</title>
</head>
<body>
<div id="webgl"></div>
</body>