Which jQuery events do not bubble?

半世苍凉 提交于 2020-06-27 17:29:06

问题


Pretty straightforward, I've been searching high and low for a comprehensive list of all jQuery events that do not bubble but I have been unable to find one.

For example I am looking for events like image.load, XMLHttpRequest.error, etc.


回答1:


The docs for jQuery's on() method says this (under "Additional Notes");

The focus and blur events are specified by the W3C to not bubble, but jQuery defines cross-browser focusin and focusout events that do bubble. When focus and blur are used to attach delegated event handlers, jQuery maps the names and delivers them as focusin and focusout respectively. For consistency and clarity, use the bubbling event type names.

In all browsers, the load, scroll, and error events (e.g., on an <img> element) do not bubble. In Internet Explorer 8 and lower, the paste and reset events do not bubble. Such events are not supported for use with delegation, but they can be used when the event handler is directly attached to the element generating the event.

I've bolded the most relevant sentences for emphasis.

Earlier in the docs (under "direct and delegated events", it also says this:

In Internet Explorer 8 and lower, a few events such as change and submit do not natively bubble but jQuery patches these to bubble and create consistent cross-browser behavior.

You'll also find that mouseleave and mouseenter do not bubble (use the mouseover and mouseout events for that). This is discussed in their respective docs.

I wouldn't begin to call the above list comprehensive, but it's a start. I'm not sure how XMLHttpRequest comes into it in your question; it isn't part of the DOM, therefore doesn't have any ancestors, therefore cannot really bubble in the same way as normal DOM events.




回答2:


According to DOM Level 3 Events spec,

  • Events that bubble:

    beforeinput, click, compositionstart, compositionupdate, compositionend, dblclick, focusin, focusout, input, keydown, keyup, mousedown, mousemove, mouseout, mouseover, mouseup, select, wheel.

  • Events that don't bubble:

    abort, blur, error, focus, load, mouseenter, mouseleave, resize, scroll(*), unload

Note DOM Level 3 Events doesn't define all events, some of them are defined by HTML5 spec.

That spec is less clear, because doesn't always say if events bubble and defines some events in non-normative sections. In one of these it says that drag-and-drop events bubble:

dragstart, drag, dragenter, dragexit, dragleave, dragover, drop, dragend.


(*) When dispatched on the Document element, scroll event type MUST bubble to the defaultView object.



来源:https://stackoverflow.com/questions/25556817/which-jquery-events-do-not-bubble

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!