Javascript 检测 HTML 元素上可用的事件处理程序
有没有办法检测 HTML 元素本机可用的事件处理程序?
例如:
isAvailable(img.onload) === true; // All browsers
isAvailable(script.onload) === true; // Non-IE only (Webkit, Firefox, Opera)
isAvailable(link.onload) === true; // IE (and I think Opera) only
理想情况下,我想在我的脚本中进行功能检测,如果 onload
可用于元素使用它,否则回退。目前我必须进行浏览器分叉(基于 IE),这很烦人,因为 IE 可能开始支持 script.onload
,而 Webkit/Firefox 可能开始支持 link.onload
。
不幸的是,分配 element.onload
会使事件不再“未定义”,无论它最终是否会触发。
Is there a way to detect what event handlers are available natively for an HTML element?
For example:
isAvailable(img.onload) === true; // All browsers
isAvailable(script.onload) === true; // Non-IE only (Webkit, Firefox, Opera)
isAvailable(link.onload) === true; // IE (and I think Opera) only
Ideally I want to do feature detection in my script, where if onload
is available for an element to use that, otherwise fallback. Currently I am having to do browser forks (based on IE) which is annoying as IE may start supporting script.onload
, and Webkit/Firefox may start supporting link.onload
.
Unfortunately assigning element.onload
makes the event no longer 'undefined', regardless of whether it will eventually fire or not.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
(编辑见下文,这不起作用。)您可以检查该元素是否具有
onload
属性:在IE7上,我得到
true
对于img
,false
对于script
。编辑 这不适用于 Firefox。留下这个只是为了让其他人不要走同样的路。
(Edit See below, this doesn't work.) You can check whether the element has an
onload
property:On IE7, I get
true
for theimg
, andfalse
for thescript
.Edit This doesn't work for Firefox. Leaving this just so others don't go down the same path.
我不确定这是否是您所要求的,但这会让您知道您是否有可用于给定对象的特定方法或属性。
编辑:重新编写答案以显示示例。
I am not sure if this is what you were asking for, but this will let you know if you have a specific method or property available for a given object.
Edit: Re-worked the answer to show an example.
我以前做过类似的事情;当在 iPhone 上为手机间隙编写内容时,根据您是否在模拟器中或在不同版本的设备上运行应用程序,您通常有不同的处理程序来处理输入按钮(以及大多数其他事情)的点击 - 所以在顶部我只是对我的脚本进行快速检查;
然后我可以继续绑定我的事件处理程序;
I've done something like this before; when writing stuff for phone gap on the iphone, depending if you run the app in the simulator or on different versions of the device, you often have different handlers for handling the click of input buttons (and most other things)- so at the top of my script i just do a quick check;
then i can go ahead and bind my event handler;
以下是从 Modernizr 进行事件检测的方式中提取的示例:
Here's an example destilled from the way Modernizr does event detection:
我过去这样做的一种方法是使用旧的“for in”循环,并检查每个键值以查看它是否以“on”开头(我见过的每个本机事件处理程序都是这样开始的。 .)所以,例如:
瞧!现在您知道可以在该元素上注册哪些事件处理程序了!
One way I've done this in the past is to use the old "for in" loop, and check each key value to see if it starts with "on" (every native event handler I've ever seen starts this way...) So, for example:
voila! Now you know what event handlers can be registered on that element!
试试这个:
您还可以循环遍历 div 的(或采用任何其他 HTML 元素)属性来动态检查它:
您可以在 mozilla 的开发博客
Try this one:
you could also loop through div's(or take any other HTML element) properties to dynamically check it:
You could check more on hasOwnProperty on mozilla's dev blog
有一些关于此事件支持的虚假报告,甚至来自像...这样的退伍军人,我们不提名字 - 但不明显的是,onload 事件很可能不会在 IMG 元素、SCRIPT 元素和类似元素上触发,因为来源已经是cashed 和从现金中提取资源的元素不会触发 onload 事件。
例外:即使在处理已兑现的文件时,文档元素也会触发 onload 事件,因为它取决于就绪状态完成。
There are, false reports on this event support even from veterans like..., let's not mention names - but it is NOT obvious that the onload event will most probably not fire on IMG elements SCRIPT elements and similar, because the source has already been cashed and Elements whose resources are being drawn from the cash will not fire the onload event.
Exception: the document element will fire the onload event, even when working with cashed files, because it depends on readystate complete.