Is it really necessary to wait for DOM ready to manipulate the DOM?

前端 未结 4 771
北海茫月
北海茫月 2020-12-11 08:03

Is it really necessary to wait for the \"ready\" (or \"window.onload\") events if your code only manipulates DOM elements that have already been parsed entirely?

The

4条回答
  •  佛祖请我去吃肉
    2020-12-11 08:46

    in your case it is fine because the browser will render your code line by line and in your code id="foo" comes first so it will get that div....but suppose you wrote this script in head tag then the script will run first and it wont get the div with id="foo" because its not yet loaded..its better to write in document.ready method

提交回复
热议问题