检测对事件类型的支持 [英] Detecting support for event types

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

问题描述

使用createEvent时,必须提供eventType参数作为

参数。这可以是DOM 2或3事件中指定的那些之一,或者

它可能是专有的eventType [1]。


我的问题是测试支持特定的eventTypes - DOM 2

事件接口DocumentEvent说如果eventType不支持
,它会抛出一个DOM异常[2]。这使得测试非常困难 - 如果你尝试类似的东西:


if(document&& document.createEvent&&

document.createEvent(eventType)){...}

或类型测试和eventType(比如''MouseEvents'')不是
,脚本崩溃,并按照

DOM 2事件规范中的规定进行DOM执行。我可以试试..但是我宁愿不要,如果我可以

避免它:


var eventType =''MouseEvents'';

尝试{

document.createEvent(eventType);

alert(eventType +''OK'');

} catch(e){

alert(createEvent''不支持eventType +'');

返回;

}

是否有可以使用的功能测试?

1. Gecko支持事件类型:

< URL: http://developer.mozilla.org/en/docs...nt.createEvent

When using createEvent, an eventType parameter must be provided as an
argument. This can be one of those specified in DOM 2 or 3 Events, or
it might be a proprietary eventType[1].

My problem is testing for support of particular eventTypes - the DOM 2
Events Interface DocumentEvent says that if the eventType is not
supported, it throws a DOM exception[2]. This makes testing rather
tough - if you try something like:

if (document && document.createEvent &&
document.createEvent(eventType)) {...}

or a typeof test and the eventType (say ''MouseEvents'') isn''t
supported, the script crashes with a DOM execption as specified in the
DOM 2 Events spec. I can do try..catch but I''d rather not if I can
avoid it:

var eventType = ''MouseEvents'';
try {
document.createEvent(eventType);
alert(eventType + '' OK'');
} catch (e) {
alert(eventType + '' not supported by createEvent'');
return;
}
Is there a feature test that can be used?
1. Gecko supporte eventTypes:
<URL: http://developer.mozilla.org/en/docs...nt.createEvent


>
>



2. W3C DOM 2事件:接口DocumentEvent - createEvent

< URL: http://www.w3.org/TR/DOM-Level -2-Eve ... nt-createEvent


>
>



-

Rob


--
Rob

推荐答案

8月15,2:04 * pm,RobG< rg ... @ iinet.net.auwrote:
On Aug 15, 2:04*pm, RobG <rg...@iinet.net.auwrote:

使用createEvent时,必须提供eventType参数作为

参数。
When using createEvent, an eventType parameter must be provided as an
argument.



当我在它的时候,每个事件类型都有一个属于

的事件列表(例如点击, mouseup和mousedown属于

MouseEvents,而焦点和模糊属于UIEvents),或者我是否必须猜测它们并通过反复试验进行测试?

-

Rob

While I''m at it, is there a list somewhere of the events that belong
to each event type (e.g. click, mouseup and mousedown belong to
MouseEvents while focus and blur belong to UIEvents), or do I have to
guess them and test by trial and error?
--
Rob


RobG写道:
RobG wrote:

虽然我在它,是否有一个列表的事件属于每个事件类型的
(例如点击,mouseup和mousedown属于

MouseEvents焦点和模糊属于UIEvents),还是我必须猜测它们并通过反复试验进行测试?
While I''m at it, is there a list somewhere of the events that belong
to each event type (e.g. click, mouseup and mousedown belong to
MouseEvents while focus and blur belong to UIEvents), or do I have to
guess them and test by trial and error?



W3C DOM Level 2 Events规范列出了事件,例如参见
http://www.w3.org/TR/DOM-Level-2-Eve...Events -UIEvent

然后你会发现DOMFocusIn,DOMFocusOut和DOMActivate。


-


Martin Honnen
http://JavaScript.FAQTs.com/


8月14日9:04 * pm,RobG< rg ... @ iinet.net.auwrote:
On Aug 14, 9:04*pm, RobG <rg...@iinet.net.auwrote:

使用createEvent时,必须将eventType参数作为

参数提供。 *这可以是DOM 2或3事件中指定的那个之一,或者

它可能是专有的eventType [1]。


我的问题是测试支持特定的eventTypes - DOM 2

事件接口DocumentEvent说如果eventType不支持
,它会抛出一个DOM异常[2]。 *这使得测试非常困难 - 如果你尝试类似的东西:


* if(document&& document.createEvent&&

document.createEvent(eventType)){...}

或类型测试和eventType(说''MouseEvents'')不是<支持
,脚本崩溃,并按照

DOM 2事件规范中的规定进行DOM执行。 *我可以尝试..但是我不愿意,如果我可以

避免它:


* var eventType =''MouseEvents'' ;

*尝试{

* * document.createEvent(eventType);

* * alert(eventType +''OK'');

*} catch(e){

* * alert(createEvent''不支持eventType +'');

* * return ;

*}


是否有可以使用的功能测试?
When using createEvent, an eventType parameter must be provided as an
argument. *This can be one of those specified in DOM 2 or 3 Events, or
it might be a proprietary eventType[1].

My problem is testing for support of particular eventTypes - the DOM 2
Events Interface DocumentEvent says that if the eventType is not
supported, it throws a DOM exception[2]. *This makes testing rather
tough - if you try something like:

* if (document && document.createEvent &&
document.createEvent(eventType)) {...}

or a typeof test and the eventType (say ''MouseEvents'') isn''t
supported, the script crashes with a DOM execption as specified in the
DOM 2 Events spec. *I can do try..catch but I''d rather not if I can
avoid it:

* var eventType = ''MouseEvents'';
* try {
* * document.createEvent(eventType);
* * alert(eventType + '' OK'');
* } catch (e) {
* * alert(eventType + '' not supported by createEvent'');
* * return;
* }

Is there a feature test that can be used?



这里的第二段


< URL:http://www.w3.org/TR/DOM-Level -2-Events / events.html#Events-eventgroupings-mouseevents>


表示以下内容足够


if(typeof document ==''object''&& document&&

document.implementation&&

document.implementation.hasFeature&&& br />
document.implementation.hasFeature(''MouseEvents'',2.0))


但浏览器可能仍然在撒谎,因为这意味着

MouseEvents接口存在*和*有效。有证据表明

许多功能都存在于浏览器中但不起作用。这可能太过了。

偏执。


另见Flanagan'的第5版第314页获取模块名称列表

可以用于hasFeature,事件部分中的页面404,以及参考部分中的页面776中的
。 (Flanagan的第4版有

相同的内容索引。)


彼得

Second paragraph here

<URL: http://www.w3.org/TR/DOM-Level-2-Events/events.html#Events-eventgroupings-mouseevents>

indicates something like the following would be sufficient

if (typeof document == ''object'' && document &&
document.implementation &&
document.implementation.hasFeature &&
document.implementation.hasFeature(''MouseEvents'', 2.0))

But the browser may well still be lying as this implies the
MouseEvents interface is present *and* works. Evidence has shown that
many features are present in browsers but don''t work. This may be too
paranoid.

See also Flanagan''s 5th edition page 314 for a list of module names
that can be used for hasFeature, page 404 in the events section, and
page 776 in the reference section. (Flanagan''s 4th edition has the
same content indexed.)

Peter


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

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