jQuery中常见的两种入口函数:
第一种: 简单易用。 1 $(function () { 2 ... // 此处是页面 DOM 加载完成的入口 3 }) ;
第二种: 繁琐,但是也可以实现 1 $(document).ready(function(){ 2 ... // 此处是页面DOM加载完成的入口 3 });
总结
等着 DOM 结构渲染完毕即可执行内部代码,不必等到所有外部资源加载完成,jQuery 帮我们完成了封装。
相当于原生 js 中的 DOMContentLoaded。
不同于原生 js 中的 load 事件是等页面文档、外部的 js 文件、css文件、图片加载完毕才执行内部代码。
更推荐使用第一种方式。