DOM: Is it possible for user interaction to occur on anything but an element node?

StackOverflow https://stackoverflow.com/questions/21324850

  •  02-10-2022
  •  | 
  •  

Pregunta

In the W3C browser spec, there's a lot of user interaction events. Keypress, click, mouse move, etc.

Most of which occur on the window object (mousemove), but some of which occur on elements (click).

Is there any time an event occurs on anything but the window object or an element?

For instance, is it possible for a click event to be dispatched on a text node by the browser?

Is there a list of which node types correspond with which event types?

¿Fue útil?

Solución

Is there any time an event occurs on anything but the window object or an element?

DOM 3 Event Types can occur on document and defaultView objects and there is a long list of Legacy Event Types that can occur on a much wider range of node types.

While the vast majority of DOM 3 events have an element as their event target, it should not be assumed that the event target is always an element. Any type of node can dispatch an event (i.e. can be an event target). Of the 12 node types for Interface Node, 11 aren't an element.

The W3C DOM specifications define the types of event associated with various types of node, but that does not limit the type of node that can be an event target:

Note: Though an event listener can be registered for any event target node, the user agent only dispatches UA-generated (trusted) events on node types that are defined as event target types for that specific event type (see the List of DOM3 Event Types). For example, a mouseover event type registered on a text node will never be fired by the user agent, though a content author could dispatch an event of that type on the text node via script.

Document Object Model (DOM) Level 3 Events Specification (Draft) §4.3

So even if the W3C doesn't specify standard events for non–element nodes, they may in the future and content authors (developers) can dispatch events from non–element nodes already (and have been able to do that for some time).

Also, browsers are not constrained to only those events in the W3C specifications, they can do what they like.

is it possible for a click event to be dispatched on a text node by the browser

Yes, and early versions of Safari did just that. However is was not conformant with other browsers so it was changed to comply with common behaviour.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top