如何解决Uncaught TypeError:无法读取null的属性'addEventListener'?

时间:2019-01-30 07:39:23

标签: javascript

当我将addEventlistener用于DOMContentLoaded时,控制台出现错误,指出无法读取null的属性'addEventListener'。 并请向我解释如何解决此typeError?

window.addEventListener('DOMContentLoaded', function(){
    var canvas = document.getElementById('canvas');

    var engine = new BABYLON.Engine(canvas, true);
    engine.enableOfflineSupport = false; // Dont require a manifest file
    var createScene = function(){
        var scene = new BABYLON.Scene(engine);
        scene.clearColor = new BABYLON.Color3.White();


        var camera = new BABYLON.ArcRotateCamera("arcCam",
                BABYLON.Tools.ToRadians(0),
                BABYLON.Tools.ToRadians(0),
                10.0,BABYLON.Vector3.Zero(),scene);
        camera.attachControl(canvas,true);
        var light = new BABYLON.PointLight("PointLight",new BABYLON.Vector3(
        0,0,0),scene);
        light.parent = camera;
        light.intensity = 1.5;

        BABYLON.SceneLoader.ImportMesh("","","ShippingContainer.babylon",
        scene,function(newMeshes) {
            newMeshes.forEach(function(mesh){
                mesh.rotation = new BABYLON.Vector3(BABYLON.Tools.ToRadians(
                45),0,0);
            }                );
        });

        return scene;
    }

    var scene = createScene();
    engine.runRenderLoop(function(){
        scene.render();
    });

});

未捕获的TypeError:无法读取null的属性'addEventListener'

1 个答案:

答案 0 :(得分:0)

检查您的HTML文件。将您的HTML文件与JS文件链接的脚本行将位于头部。将其移至身体末端,此问题将消失。