jQuery 事件处理API
click()事件
focus() 和 blur()事件不支持冒泡,focusin和focusout支持
mouseenter和mouseleave不支持冒泡 mouseover和mouseout支持
hover()方法用来给mouseenter和mouseleave事件注册处理程序
hover(handlerIn,handleOut) : Bind two handlers to the matched elements, to be executed when the mouse pointer enters and leaves the elements.
hover(handlerInOut) : Bind a single handler to the matched elements, to be executed when the mouse pointer enters or leaves the elements.
jQuery事件的基本概念
jQuery()事件对象模拟标准的event对象
jQuery()事件处理函数的返回值始终有意义,返回false意味着调用了preventDefault()和stopPropagation(),返回的非undefined值会存储在事件对象的result中,便于后面的连续处理事件访问
jQuery事件对象的常用属性:
pageX,pageY鼠标的文档坐标
target发生事件的文档元素 currentTarget当前正在执行的事件处理程序所注册的元素 relatedTarget在鼠标悬浮及离开事件时,表示鼠标指针移开的元素
timestamp事件发生的时间
data注册事件处理程序时指定的额外数据
handler当前被调用的事件处理程序的函数引用
result函数的返回值
事件方法
jQuery()的事件触发机制是同步的,不涉及事件队列
.bind()已弃用改为.on() : Attach an event handler function for one or more events to the selected elements.
.unbind()被替换为.off() :Remove an event handler.
trigger()触发事件 :Execute all handlers and behaviors attached to the matched elements for the given event type.
- The
.triggerHandler( "event" )
method will not call.event()
on the element it is triggered on. This means.triggerHandler( "submit" )
on a form will not call.submit()
on the form. - While
.trigger()
will operate on all elements matched by the jQuery object,.triggerHandler()
only affects the first matched element. - Events triggered with
.triggerHandler()
do not bubble up the DOM hierarchy; if they are not handled by the target element directly, they do nothing. - Instead of returning the jQuery object (to allow chaining),
.triggerHandler()
returns whatever value was returned by the last handler it caused to be executed. If no handlers are triggered, it returnsundefined