跳到主要内容

一、作用

二、判定事件是否被支持

备注
/**
* Checks if an event is supported in the current execution environment.
* 检查当前执行环境是否支持某个事件
*
* NOTE: This will not work correctly for non-generic events such as `change`,
* `reset`, `load`, `error`, and `select`.
* 注意:这对于非通用事件(如 `change`、`reset`、`load`、`error` 和 `select`)将无法正常工作。
*
* Borrows from Modernizr.
* 借鉴自 Modernizr
*
* @param {string} eventNameSuffix Event name, e.g. "click".
* @return {boolean} True if the event is supported.
* @internal
* @license Modernizr 3.0.0pre (Custom Build) | MIT
*/
function isEventSupported(eventNameSuffix: string): boolean {
if (!canUseDOM) {
return false;
}

const eventName = 'on' + eventNameSuffix;
let isSupported = eventName in document;

if (!isSupported) {
const element = document.createElement('div');
element.setAttribute(eventName, 'return;');
isSupported = typeof (element as any)[eventName] === 'function';
}

return isSupported;
}