在firefox中使div可拖动

时间:2012-09-14 12:25:13

标签: javascript html html5

我已经尝试了代码in this thread,但它似乎对我不起作用。我使用的是Firefox 15,它适用于Chrome。

这是我的代码:

<!DOCTYPE html>
<head>
  <title>A Simple Draggable Object</title>
</head>
<body>
    <script>
        window.onload=function() {
            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);
                 });
            }

        };
</script>

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

感谢任何帮助

1 个答案:

答案 0 :(得分:2)

window.onload=function() {
        var dragItems = document.querySelectorAll('[draggable=true]');

        for (var i = 0; i < dragItems.length; i++) {
          dragItems[i].addEventListener('dragstart', function (event) {
            event.dataTransfer.setData('Text', this.id);
          });
        }

};

为我工作。感谢Kris C