1. 程式人生 > >jQuery 事件處理API

jQuery 事件處理API

behavior over caused blank target mit ati ren data

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.

.triggleHandler()

  • 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 returns undefined

jQuery 事件處理API