次の宿題に取り組んでいるときに質問が出てきました:divs
をクリックすると色が変わる 4 人を作成します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
このプロジェクトが成長したとき、ユーザーはコードを実行する前にブラウザーが常に最初に取得する必要があることを私のソリューションで認識しますか?
フォローしていただけると幸いです。