我喜欢在div上添加背景,仅适用于支持drag& amp;删除文件
我不喜欢使用modernizr,只是一行脚本
答案 0 :(得分:9)
为什么不从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')) {
...
}
对于File API:
var isFileAPIEnabled = function() {
return !!window.FileReader;
};
答案 1 :(得分:0)
您可以使用:
return 'draggable' in document.createElement('span') && typeof(window.FileReader) != 'undefined';
答案 2 :(得分:0)
如果您根本不想处理Modernizr,您可以复制它对拖放检测的作用:
var supportsDragAndDrop = function() {
var div = document.createElement('div');
return ('draggable' in div) || ('ondragstart' in div && 'ondrop' in div);
}
从Modernizr GitHub存储库获取它:
https://github.com/Modernizr/Modernizr/blob/master/feature-detects/draganddrop.js
答案 3 :(得分:0)
结帐 modernizr HTML5拖放检测的源代码技术https://github.com/Modernizr/Modernizr/blob/master/feature-detects/draganddrop.js
答案 4 :(得分:0)
除了这似乎错误地检测到iOS支持拖放
答案 5 :(得分:0)
不确定为什么每个人都需要创建一个新元素来进行检查。我认为仅检查body元素是否支持拖动事件以及浏览器是否支持File API就足够了
DateTime?