Javascript - 检测是否支持事件列表 [英] Javascript - detect if event lister is supported

查看:164
本文介绍了Javascript - 检测是否支持事件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检测某些浏览器中是否支持某些事件?
我可以检测到浏览器是否支持document.addEventListener,但是我需要知道它是否支持事件DOMAttrModified。 Firefox和Opera支持它,但Chrome和其他人不支持。

Is it possible to detect if certain events are supported in certain browsers? I can detect if the browser supports document.addEventListener, but I need to know if it supports the event DOMAttrModified. Firefox and Opera support it, but Chrome and others do not.

感谢任何人可以帮助!

推荐答案

更新答案

,您可以对其进行特征检测。创建元素,侦听事件,并更改元素上的属性。在我的测试中,您甚至不需要将元素添加到DOM树中,使其成为一个很好的,包含的特征检测。

Yes, you can feature-detect this. Create an element, listen for the event, and change an attribute on the element. In my tests, you don't even have to add the element to the DOM tree, making this a nice, contained feature detection.

示例:

function isDOMAttrModifiedSupported() {
    var p, flag;

    flag = false;
    p = document.createElement('p');
    if (p.addEventListener) {
        p.addEventListener('DOMAttrModified', callback, false);
    }
    else if (p.attachEvent) {
        p.attachEvent('onDOMAttrModified', callback);
    }
    else {
        // Assume not
        return false;
    }
    p.setAttribute('id', 'target');
    return flag;

    function callback() {
        flag = true;
    }
}

Live copy

Firefox触发上述所有修改的回调;

Firefox triggers the callback on all of the modifications above; Chrome on none of them.

原始答案

您可以检测是否支持某些事件,如这个方便的页面。我不知道你能否专门测试一下,但是如果可以的话,那个代码可能会让你开始。

You can feature-detect whether some events are supported, as shown on this handy page. I don't know if you can test specifically for that one, but if you can, that code may well get you started.

更新 :我将Kangax的代码转储到JSBin 并尝试过,看起来不像那个嗅探技术适用于该事件(除非我的名称拼写不正确或某事; Firefox正在显示false)。但我上面的技巧是。

Update: I dumped Kangax's code into JSBin and tried it, doesn't look like that sniffing technique works for that event (unless I have the name spelled incorrectly or something; Firefox is showing "false"). But my technique above does.

这篇关于Javascript - 检测是否支持事件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆