Binding a handler for an event (like a simple clickevent) is such a common task that jQuery provides an even terser way to accomplish it; shorthand event methodswork in the same way as their .bind()counterparts with a few less keystrokes.
给一个事件绑定处理器,比如click事件,是一个相当常见的任务,因此jquery提供了一个更加简洁的方法来实现。事件处理快捷方式按照在bind方法中相同的方法工作,但是却可以少输入一些字母。
For example, our style switcher could be written using .click()instead of .bind()as shown in the following code snippet:
$(document).ready(function() {
$('#switcher-default').addClass('selected');
$('#switcher button').click(function() {
var bodyClass = this.id.split('-')[1];
$('body').removeClass().addClass(bodyClass);
$('#switcher button').removeClass('selected');
$(this).addClass('selected');
});
});
Listing 3.8
例如,我们的事件处理器,可以使用click而不用bind方法,正如下面的代码显示的那样。
代码同上。
Shorthand event methods, such as this, exist for all standard DOM events, as shown in the following list:
• blur
• change
• click
• dblclick
• error
• focus
• keydown
• keypress
• keyup
• load
• mousedown
• mousemove
• mouseout
• mouseover
• mouseup
• resize
• scroll
• select
• submit
• unload
事件方法的缩写为所有的标准dom事件都处理了,请看如下清单:清单同上。
Each shortcut method binds a handler to the event with the corresponding name.
每一个快捷方法都给被合适命名的事件绑定了一个处理器。