如何以编程方式确定HTML对象可以侦听哪些事件? [英] How do you programmatically determine to which events an HTML object can listen for?
问题描述
我一直在浏览developer.mozilla.org和Apple Dev文档中的文档,但是我无法找到文档,说明是否可以通过编程方式确定特定的HTML标签是否支持给定的eventListener。
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.
像我知道< script>
标签不会支持点击监听器,因为没有点击,但是我怎么知道?
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?
推荐答案
根据完美杀死:
诀窍是许多现代浏览器报告属性对应于一个元素中存在的事件名称。
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
这篇关于如何以编程方式确定HTML对象可以侦听哪些事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!