通过HTML文件输入删除文件的功能检测功能

Dan*_*kin 7 html javascript browser-feature-detection

我们可以检测浏览器是否支持删除文件<input type="file" />

例如,这可以在Chrome中实现,但在IE8中则不行.

Modernizr.draganddrop是一种可能性,但它是正确的选择吗?我没有添加任何自定义拖放事件处理程序.

更新

要验证Joe的答案,这是一个应该停止文件丢弃的jQuery示例.已在Chrome和Firefox中验证.

$yourFileInput.on('drop', function() {
    return false; // if Joe's explanation was right, file will not be dropped
});
Run Code Online (Sandbox Code Playgroud)

joe*_*ncy 2

我认为“检测对给定 JavaScript 事件的支持”的答案是什么?可能会帮助你。调整代码以针对 Input 而不是 Div 进行测试,并测试“Drop”事件似乎对我来说效果很好。

在此复制,以便您不必点击(并稍作调整,因为似乎您只需要检测这一功能):

function isEventSupported(eventName) {
  var el = document.createElement('input');
  eventName = 'on' + eventName;
  var isSupported = (eventName in el);
  if (!isSupported) {
    el.setAttribute(eventName, 'return;');
    isSupported = typeof el[eventName] == 'function';
  }
  el = null;
  return isSupported;
}
if(isEventSupported('drop')){
  //Browser supports dropping a file onto Input
} else {
  //Fall back, men!
}
Run Code Online (Sandbox Code Playgroud)