How do you programmatically determine to which events an HTML object can listen for?

 ̄綄美尐妖づ 提交于 2019-12-23 07:59:26

问题


I've been looking over the docs at developer.mozilla.org and the Apple dev docs but I'm not able to find documentation that explains whether or not you can programatically determine if a specific HTML tag supports a given eventListener.

Like I know that the <script> tag isn't going to support the click listener since there's nothing to click on, but how do I know that?

Or barring that, is there an easy reference somewhere of what events each tag supports?


回答1:


Outside of a few edge cases, yes you can, according to Perfection Kills:

The trick is that many modern browsers report property corresponding to an event name as being existent in an element.

Basically, the code looks like this:

'onclick' in document.documentElement; // true
'onclick2' in document.documentElement; // false

He uses it to detect event support in various browsers, but it could also be used to detect whether or not an element supports an event:

An event must be checked on an element that could actually originate that event.

So you also get results like this:

'onreset' in document.documentElement; // false
'onreset' in document.createElement('input'); // true



回答2:


i tell you to read this

...and the best thing you must think is that events are only for DOM objects . Then all objects in DOM , could have events (But read the list).



来源:https://stackoverflow.com/questions/7667533/how-do-you-programmatically-determine-to-which-events-an-html-object-can-listen

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