jQuery中如何使用mouseout和mouseleave?
日期:2011/11/28 來源:GBin1.com
jQuery中的鼠標事件中有mouseout和mouseleave倆個看起來類似的事件。
這倆個事件的使用有些區別,如下是jQuery文檔中的官方解析:
The mouseleave event differs from mouseout in the way it handles event bubbling. If mouseout were used in this example, then when the mouse pointer moved out of the Inner element, the handler would be triggered. This is usually undesirable behavior. The mouseleave event, on the other hand, only triggers its handler when the mouse leaves the element it is bound to, not a descendant. So in this example, the handler is triggered when the mouse leaves the Outer element, but not the Inner element.
以上定義中指出,mouseout如果應用在鑲套的元素中,當鼠標離開里面的元素將會觸發這個事件。而使用mouseleave的時候,只有鼠標離開外面的元素才會觸發mouseleave事件。
這種情況經常出現在處理ul及其鑲套的li的代碼中,當我們要執行鼠標移出ul元素的時候,有的朋友可能會使用mouseout,這個時候其實在鼠標移出li元素的時候就會觸發,因此我們需要使用mouseleave方法,演示如下:


浙公網安備 33010602011771號