在完成以下作业时出现了问题:创建 4 个divs当我单击button.
一切正常,但我的老师使用init function来获取divs通路getElementById("div1"),而我使用它changeColorFunction()本身,根本没有使用init function。见下文:
我的解决方案:
function changeColor () {
    document.getElementById("div1").style.background = createNewColor();
    document.getElementById("div2").style.background = createNewColor();
    document.getElementById("div3").style.background = createNewColor();
    document.getElementById("div4").style.background = createNewColor();
}
我的老师解决方案:
    var d1,d2,d3,d4;
    function init(){
    d1 = document.getElementById("d1");
    d2 = document.getElementById("d2");
    d3 = document.getElementById("d3");
    d4 = document.getElementById("d4");
    }
window.onload = init;
它只是一个关于获得elements. 有了这小段代码,我想差别不大,但什么时候有关系?
当这个项目增长时,用户是否会通过我的解决方案认识到浏览器总是必须先获得第elements一个才能运行代码?
希望你能跟上。