(4)事件处理——(5)为代码简洁做的快捷方式(Shortcuts for code brevity)
The $(document).ready()construct is actually calling the .ready()method on a jQuery object we've constructed from the documentDOM element. The $()function provides a shortcut for us as this is a common task. When we pass in a function as the argument, jQuery performs an implicit call to .ready(). For the same result as shown in the following code snippet:
$(document).ready(function() { // Our code here... });
We can also write the following code:
$(function() { // Our code here... });
While this other syntax is shorter, the longer version makes code more descriptive about what it is doing. For this reason, we will use the longer syntax throughout this book.
$(document).ready()结构事实上是调用了我们从一个DOM元素创建的jquery对象的.ready()方法。由于这些相同的任务,$()函数为我们提供了一个快捷方式。当我们把一个函数作为参数传递进去的时候,jquery会隐式调用.ready()方法。正如下面的代码片段造成相同的结果:
$(document).ready(function() { // Our code here... });
我们以可以写下面的代码:
$(function() { // Our code here... });
虽然另一种语法更加短,但是这个长的让更好的描述了他在做的事情。由于这个原因,我们将在整本书中使用长一些的语法。