如何检测浏览器支持文件 API 拖放 [英] How to detect browser support File API drag n drop

查看:28
本文介绍了如何检测浏览器支持文件 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;
};

这篇关于如何检测浏览器支持文件 API 拖放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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