Executing JavaScript in the <head>, getElementById returns null

前端 未结 3 1212
旧巷少年郎
旧巷少年郎 2021-01-05 00:49

I have following code





        
3条回答
  •  [愿得一人]
    2021-01-05 01:17

    It's null because you're calling the script before the DOM has been loaded.

    Wrap your script in a function which will be invoked onload, e.g.:

    window.onload = function() {
        var q = new foo();
        q.run('yellow');
    };
    

提交回复
热议问题