简体   繁体   English

您如何以编程方式确定HTML对象可以侦听哪些事件?

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

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. 我一直在查看developer.mozilla.org上的文档和Apple dev文档,但找不到能够说明您是否可以以编程方式确定特定HTML标记是否支持给定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? 就像我知道的那样, <script>标记将不支持单击侦听器,因为没有要单击的内容,但是我怎么知道呢?

Or barring that, is there an easy reference somewhere of what events each tag supports? 还是禁止这种情况,每个标签支持哪些事件?

Outside of a few edge cases, yes you can, according to Perfection Kills : 根据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

i tell you to read this 我告诉你读这篇

...and the best thing you must think is that events are only for DOM objects . ...而且您必须想到的最好的事情是,事件仅适用于DOM对象。 Then all objects in DOM , could have events (But read the list). 然后,DOM中的所有对象都可能发生事件(但请阅读列表)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM