如何检测浏览器支持文件 API 拖放
我喜欢在 div 上添加背景,仅适用于支持拖动 & 的浏览器.删除文件
I like to add a background on a div only for browsers which support drag & drop for files
虽然我不喜欢使用modernizr,只是一个单行脚本
I don't like to use modernizr though, just a one line script
推荐答案
为什么不直接从 Modernizr 复制所需的部分?
Why not just copy required parts from Modernizr?
var isEventSupported = (function() {
var TAGNAMES = {
'select': 'input', 'change': 'input',
'submit': 'form', 'reset': 'form',
'error': 'img', 'load': 'img', 'abort': 'img'
};
function isEventSupported( eventName, element ) {
element = element || document.createElement(TAGNAMES[eventName] || 'div');
eventName = 'on' + eventName;
// When using `setAttribute`, IE skips "unload", WebKit skips "unload" and "resize", whereas `in` "catches" those
var isSupported = eventName in element;
if ( !isSupported ) {
// If it has no `setAttribute` (i.e. doesn't implement Node interface), try generic element
if ( !element.setAttribute ) {
element = document.createElement('div');
}
if ( element.setAttribute && element.removeAttribute ) {
element.setAttribute(eventName, '');
isSupported = typeof element[eventName] == 'function';
// If property was created, "remove it" (by setting value to `undefined`)
if ( typeof element[eventName] != 'undefined' ) {
element[eventName] = undefined;
}
element.removeAttribute(eventName);
}
}
element = null;
return isSupported;
}
return isEventSupported;
})();
用法:
if (isEventSupported('dragstart') && isEventSupported('drop')) {
...
}
对于文件 API:
var isFileAPIEnabled = function() {
return !!window.FileReader;
};
相关文章