检测 JavaScript 中的 HTML5 拖放支持

发布于 2024-09-01 14:00:19 字数 64 浏览 1 评论 0原文

我正在尝试检测 JavaScript 中的 HTML5 拖放支持。 Modernizr 似乎无法处理这个测试用例。

I'm trying to detect the HTML5 Drag And Drop support in JavaScript. Modernizr seems to not handle this test case.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

鸠魁 2024-09-08 14:00:19

您可以执行以下操作:

if('draggable' in document.createElement('span')) {
  alert("Drag support detected");
}

您可以在此处查看使用上述检查的快速演示测试

另外,这里有一个很好的功能检测(不是浏览器检测,耶!)列表,维护得相当好以防万一您还在寻找其他 HTML5 功能。

You can do this:

if('draggable' in document.createElement('span')) {
  alert("Drag support detected");
}

You can see a quick demo test using the above check here.

Also, there's a nice feature detection (not browser detection, yay!) list that's fairly well maintained here in case you're looking for other HTML5 features as well.

烂柯人 2024-09-08 14:00:19

在 document.createElement('span') 中检测“draggable”似乎是个好主意,但实际上行不通。iOS

声称元素中存在draggable,但不允许拖放。(参考:Safari Web 内容指南:处理事件

IE9 声称元素中没有可拖动功能,但允许拖放(参考:我在 IE 中测试 HTML5 拖放。)

Modernizr 是一个更好的选择,因为它不会混淆 IE。但是,它声明 HTML5 拖放可以在 iOS 上使用。

以下是我检测 HTML5 拖放的方法:

var iOS = !!navigator.userAgent.match('iPhone OS') || !!navigator.userAgent.match('iPad');
if (Modernizr.draganddrop && !iOS) {
    HTML5 drag and drop solution
} else if (Modernizr.draganddrop && iOS) {
    iOS drag and drop solution 
} else {
    non-HTML5 drag and drop solution
}

Detecting "draggable' in document.createElement('span') seems like a good idea, but in practice it doesn't work.

iOS claims that draggable is in the element but doesn't allow drag and drop. (Reference: Safari Web Content Guide: Handling Events)

IE9 claims that draggable is NOT in the element, but does allow drag and drop. (Reference: my testing HTML5 drag and drop in IE.)

Modernizr is a better choice because it doesn't confuse IE. However, it states that HTML5 drag and drop is available on iOS.

Here's how I detect HTML5 drag and drop:

var iOS = !!navigator.userAgent.match('iPhone OS') || !!navigator.userAgent.match('iPad');
if (Modernizr.draganddrop && !iOS) {
    HTML5 drag and drop solution
} else if (Modernizr.draganddrop && iOS) {
    iOS drag and drop solution 
} else {
    non-HTML5 drag and drop solution
}

这就是它在 Modernizr 中的实现方式

function() {
    var div = document.createElement('div');
    return ('draggable' in div) || ('ondragstart' in div && 'ondrop' in div);
}

This is how it's implemented in Modernizr

function() {
    var div = document.createElement('div');
    return ('draggable' in div) || ('ondragstart' in div && 'ondrop' in div);
}
痴骨ら 2024-09-08 14:00:19

使用 Modernizr 中的“isEventSupported”方法检查是否支持“dragstart”和“drop”。请参阅如何检测浏览器支持文件 API 拖放< /a>.

Check if "dragstart" and "drop" are supported, using the "isEventSupported" method in Modernizr. See How to detect browser support File API drag n drop.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文