用document.onreadystatechange和document.readyState确保文档加载完毕才获取DOM

document.onreadystatechange = function(){
        //document.readyState有“interactive”和“complate”两个值
    if(document.readyState == "interactive" ){
        return false;
    };
    
        //从这里开始获取dom
    var dom = document.getElementById("canvas");
        console.log(dom);
    
    
};
原文地址:https://www.cnblogs.com/macliu/p/5583581.html