如果在所有页面HTML之后都加载jQuery脚本,是否仍然需要$ {document).ready? [英] Do jQuery scripts still need $(document).ready if they are loaded after all of the page HTML?

查看:74
本文介绍了如果在所有页面HTML之后都加载jQuery脚本,是否仍然需要$ {document).ready?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我将我的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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆