注:本文内容摘录自《Learning jQuery (4th Edition)》。
To react to ser behavior, we need to register our handlers using these event methods. Note that many DOM events only apply to certain element types; these subtleties are not covered here.
目录
Binding
Event method | Description |
---|---|
.ready(handler) | Binds handler to be called when the DOM and CSS are fully loaded. |
.on(type, [selector], [data], handler) | Binds handler to be called when the given type of event is sent to the element. If selector is provided, performs event delegation. |
.off(type, [selector], [handler]) | Removes bindings on the element. |
.bind(type, [data], handler) | Binds handler to be called when the given type of event is sent to he element. In general, use .on() instead. |
.one(type, [data], handler) | Binds handler to be called when the given type of event is sent to the element. Removes the binding when the handler is called. |
.unbind([type], [handler]) | Removes the bindings on the element (for an event type, a particular handler, or all bindings). |
.delegate(selector, type, [data], handler) | Binds handler to be called when the given type of event is sent to a descendant element matching selector. |
.delegate(selector, handlers) | Binds a collection of handlers to be called when the given types of events are sent to a descendant element matching selector. |
.undelegate(selector, type, [handler]) | Removes the bindings on the element previously bound with .delegate(). |
Shorthand binding
Event method | Description |
---|---|
.blur(handler) | Binds handler to be called when the element loses keyboard forcus. |
.change(handler) | Binds handler to be called when the element’s value changes. |
.click(handler) | Binds handler to be called when the element is clicked. |
.dblclick(handler) | Binds handler to be called when the element is double-clicked. |
.error(handler) | Binds handler to be called when the element receives an error event (browser-dependent). |
.focus(handler) | Binds handler to be called when the element gains keyboard focus. |
.focusin(handler) | Binds handler to be called when the element, or a descendant, gains keyboard focus. |
.focusout(handler) | Binds handler to be called when the element, or a descendant, loses keyboard focus. |
.keydown(handler) | Binds handler to be called when a key is pressed and the element has keyboard forcus. |
.keypress(handler) | Binds handler to be called when a keystroke occurs and the element has keyboard focus. |
.keyup(handler) | Binds handler to be called when a key is released and the element has keyboard focus. |
.load(handler) | Binds handler to be called when the element finishes leading. |
.mousedown(handler) | Binds handler to be called when the mouse button is pressed within the element. |
.mouseenter(handler) | Binds handler to be called when the mouse pointer enters the element. Not affected by event bubbling. |
.mouseleave(handler) | Binds handler to be called when the mouse pointer leaves the element. Not affected by event bubbling. |
.mousemove(handler) | Binds handler to be called when the mouse pointer moves within the element. |
.mouseout(handler) | Binds handler to be called when the mouse pointer leaves the element. |
.mouseover(handler) | Binds handler to be called when the mouse pointer enters the element. |
.mouseup(handler) | Binds handler to be called when the mouse button is released within the element. |
.resize(handler) | Binds handler to be called when the element is resized. |
.scroll(handler) | Binds handler to be called when the element;s scroll position changes. |
.select(handler) | Binds handler to be called when text in the elemet is selected. |
.submit(handler) | Binds handler to be called when the form element is submitted. |
.unload(handler) | Binds handler to be called when the element is unloaded from memory. |
.hover(enter, leave) | Binds enter to be called when the mouse enters the element, and leave to be when the mouse leaves it. |
Triggering
Event method | Description |
---|---|
.trigger(type, [data]) | Triggers handlers for the vent on the element and executes the default action for the event. |
.triggerHandler(type, [data]) | Triggers handlers for the event on the element without executing any default actions. |
Shorthand triggering
Event method | Description |
---|---|
.blur() | Triggers the blur event. |
.change() | Triggers the change event. |
.click() | Triggers the click event. |
.dblclick() | Triggers the dblclick event. |
.error() | Triggers the error event. |
.focus() | Triggers the focus event. |
.keydown() | Triggers the keydown event. |
.keypress() | Triggers the keypress event. |
.keyup() | Triggers the keyup event. |
.select() | Triggers the select event. |
.submit() | Triggers the submit event. |
Utility
Event method | Description |
---|---|
$.proxy(fn, context) | Creates a new function that executes with the given context. |