开发者问题收集

如何解决未捕获的类型错误:无法读取 null 的属性“addEventListener”?

2019-01-30
998

当我对 DOMContentLoaded 使用 addEventlistener 时,控制台中出现错误,提示无法读取 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个回答

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

Sones1
2020-06-15