本文介绍了Safari 中的 EventTarget 接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始通过添加一些有用的 prototypes 来扩展 EventTarget 接口,但后来我在 Safari 8 上对其进行了测试并得到:

I started to extend the EventTarget interface by adding some useful prototypes but then I tested it on Safari 8 and get :

[Error] ReferenceError: Can't find variable: EventTarget

我在 MDN 上发现 "window.EventTarget 不存在" for Safari.

I found on MDN that "window.EventTarget does not exist" for Safari.

这个问题看起来很有趣,但它是关于 IE8 的.

This question looks really interesting but it's about IE8.

所以,我想知道是否可以在 Safari 上访问对 EventTarget 接口的引用,或者是否有使用 EventTarget.prototype.hasEventListener 等代码的解决方法>EventTarget.prototype.hasEventListener 在 Safari 上没有任何错误?

So, I want to know if a reference to the EventTarget interface is accessible on Safari, or a workaround to use code like EventTarget.prototype.hasEventListener and EventTarget.prototype.hasEventListener without getting any errors on Safari ?

编辑我发现了一个有趣的 commit 说它是在 09/12/15 实现的(来自时间戳),但它肯定在 Safari 9.1 中不起作用

EditI found an interesting commit which says that it was implemented the 09/12/15 (from timestamp), but it sure doesn't work in Safari 9.1

推荐答案

解决方法:

我只是使用了Element 接口 作为 Safari 的后备

Workaround :

I simply used the Element interface as a fallback for Safari

var EventTarget = EventTarget || Element;
EventTarget.prototype.addEventListener = function(){/*Some magic here*/};

我还检查了 Element 从 EventTarget 接口继承了 prototypes 并且确实如此!(document.body.addEventListener == EventTarget.prototype.addEventListener 返回 true)

I also checked that Element inherits prototypes from the EventTarget interface and it does ! (document.body.addEventListener == EventTarget.prototype.addEventListener returned true)

这篇关于Safari 中的 EventTarget 接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 08:39