如果在所有页面HTML之后都加载jQuery脚本,是否仍然需要$ {document).ready? [英] Do jQuery scripts still need $(document).ready if they are loaded after all of the page HTML?
问题描述
如果我将我的jQuery脚本加载到我所有页面HTML的下方,我是否还需要等待$(document).ready
能够使用jQuery在页面中查找元素?
If I'm loading my jQuery scripts below all of my page HTML, do I still need to wait for $(document).ready
to be able to use jQuery to find elements in the page?
推荐答案
否,因为文档已经被加载. Dom从上到下加载.我个人喜欢将所有js放在页面底部而不是头部.
No because the document would have already been loaded. The Dom loads top to bottom. I personally like to put all my js at the bottom of the page instead of in the head.
但是,它只有1行代码,为了安全起见,我建议您使用它.您也可以使其更短. $(function() {}
与$(document).ready(function(){})
however it is only 1 line of code and i would suggest using it just to be safe. also you can make it even shorter. $(function() {}
is the same as $(document).ready(function(){})
这篇关于如果在所有页面HTML之后都加载jQuery脚本,是否仍然需要$ {document).ready?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!