Html 如何使 HTML5 中的 <div> 可在 Firefox 中拖动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3977596/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:54:43  来源:igfitidea点击:

How to make <div>s in HTML5 draggable for Firefox?

firefoxhtmldraggable

提问by HdM

I am playing around with the HTML5 features, and I want div's (and similar containers like articles, sections, etc.) to be draggable. Consider the following code:

我正在尝试使用 HTML5 功能,并且我希望 div(以及类似的容器,如文章、部分等)是可拖动的。考虑以下代码:

<!DOCTYPE html>
<html>
<head>
  <title>A Simple Draggable Object</title>
</head>

<body>
    <h1>Test #1: A Simple Draggable Object</h1>
    <div draggable="true">This text should be draggable.</div>
</body>
</html>

I tested in OS X the following browsers: In Chrome 7.0 and Safari 5.0.2 I can successfully drag the text around, but in Firefox 3.6 and 4.0b6 I can neither drag the text nor mark it (as if it was usual text). Is this a bug or a feature? How do I achieve that Firefox lets me drag around these tags withoutusing jQuery ?

我在 OS X 中测试了以下浏览器: 在 Chrome 7.0 和 Safari 5.0.2 中,我可以成功地拖动文本,但在 Firefox 3.6 和 4.0b6 中,我既不能拖动文本,也不能对其进行标记(就好像它是普通文本一样)。这是错误还是功能?我如何实现 Firefox 让我在使用 jQuery 的情况下拖动这些标签?

回答by Skilldrick

According to HTML5 Doctor, this won't work in Firefox without some JS help.

根据HTML5 Doctor,如果没有一些 JS 帮助,这将无法在 Firefox 中工作。

The HTML 5 spec says it should be as simple as adding the following attributes to the markup of the elements in question:

draggable="true"

However, this doesn't work completely for Safari or Firefox. For Safari you need to add the following style to the element:

[draggable=true] {
  -khtml-user-drag: element;
}

This will start working in Safari, and as you drag it will set a default, empty value with the dataTransfer object. However, Firefox won't allow you to drag the element unless you manually set some data to go with it. To solve this, we need a dragstart event handler, and we'll give it some data to be dragged around with:

var dragItems = document.querySelectorAll('[draggable=true]');

for (var i = 0; i < dragItems.length; i++) {
  addEvent(dragItems[i], 'dragstart', function (event) {
    // store the ID of the element, and collect it on the drop later on

    event.dataTransfer.setData('Text', this.id);
  });
}

HTML 5 规范说它应该像将以下属性添加到相关元素的标记一样简单:

draggable="true"

但是,这并不完全适用于 Safari 或 Firefox。对于 Safari,您需要向元素添加以下样式:

[draggable=true] {
  -khtml-user-drag: element;
}

这将在 Safari 中开始工作,当您拖动它时,它将使用 dataTransfer 对象设置一个默认的空值。但是,Firefox 不允许您拖动元素,除非您手动设置一些数据以配合它。为了解决这个问题,我们需要一个 dragstart 事件处理程序,我们将给它一些要拖动的数据:

var dragItems = document.querySelectorAll('[draggable=true]');

for (var i = 0; i < dragItems.length; i++) {
  addEvent(dragItems[i], 'dragstart', function (event) {
    // store the ID of the element, and collect it on the drop later on

    event.dataTransfer.setData('Text', this.id);
  });
}