HTML5拖放不能在IE11上运行

时间:2013-08-05 19:09:27

标签: javascript html internet-explorer drag-and-drop

应用HTML5原生拖放功能,drop不适用于IE,与chrome和firefox配合使用。

拖动似乎正在起作用,但对IE不感兴趣。

另一个小问题 - 在IE中,我的可拖动元素周围有一个半透明的方块,但它的背景是透明的(图像是这样完成的),在chrome / firefox上我没有那个方块,图像看起来没有任何拖动时的背景。

这是下降区域:

<div id="4x2" class="dropArea" draggable="false" ondragenter="drag_enter(event); return false;" ondrop="drag_drop(event); return false;" ondragover="return false" ondragleave="drag_leave(event); return false;" data-droppable="true" onmouseover="return mouseOver(this); return false;" onclick="return movePlayer(this); return false;" onmouseout="return mouseOut(this); return false;">
</div>

这是可拖动的元素:

<div id="player1" draggable="true" ondragstart="drag_start(event); return false;" ondragend="drag_end(event); return false;" data-droppable="false" onclick="return selectPlayer(this); return false;" data-selectable="true"></div>
function drag_start(e) 
    {
        e.dataTransfer.effectallowed = 'copy';
        e.dataTransfer.dropEffect = 'copy';
        e.dataTransfer.setData("text/plain", e.target.getAttribute('id'));
    }

function drag_enter(e) {

        if (e.target.getAttribute('data-droppable') == 'true') {
            e.target.style.backgroundImage = "url(images/board_cell_background_highlight.png)";
        }

function drag_leave(e) {

        if (e.target.getAttribute('data-droppable') == 'true') {
            e.target.style.backgroundImage = "url(images/board_cell_background.png)";
        }


function drag_drop(e) {
        var element = e.dataTransfer.getData("Text"); // the player
        if (e.preventDefault) {
            e.preventDefault();
        }
        if (e.stopPropagation) {
            e.stopPropagation();
        }
        if (e.target.getAttribute('id') == "player1" || e.target.getAttribute('id') == "player2") {
            alert("invalid Move");
            return false;
        }

        e.target.style.backgroundImage = "url(images/board_cell_background.png)";
        moveHandler(element, e.target.getAttribute('id'));
    }

function drag_end(e) {
        e.dataTransfer.effectallowed = 'copy';
        alert("drop end")
        }
    }
}

我删除了一些打印内容的代码,以使代码更短。

4 个答案:

答案 0 :(得分:3)

您正在设置text/plain类型的数据,但检索类型为Text的数据。虽然有些浏览器可能会理解它们是同一个,但其他浏览器可能不会。在这种情况下,似乎Internet Explorer很迂腐,而Chrome和Firefox则不严格。

就个人而言,我建议使用Text。这可能是旧的,但是如果事件处理有一些小的调整,即使早在IE5,它也可以使它工作正常。

答案 1 :(得分:3)

如果有人在Internet选项安全性选项卡中没有拖放11中的IE 8.1 W并删除复选标记框保护模式或以管理员身份运行IE

答案 2 :(得分:2)

IE10 / 11使用Text作为数据字符串,如果使用text / plain则会中断。 如果您使用文本,它会在Firefox中中断。

通过在我需要写的任何拖放功能中执行类似的操作来解决这个问题:

var setDataString = 'text/html';
// We need to change the setDataString type for IE since IE doesn't support setData and getData correctly. 
this.changeDataStringForIe = (function() {
    var userAgent = window.navigator.userAgent,
    msie = userAgent.indexOf('MSIE '),       //Detect IE
    trident = userAgent.indexOf('Trident/'); //Detect IE 11

    if (msie > 0 || trident > 0) {
        setDataString = 'Text';
        return true;
    } else {
        return false;
     }
})();

我很想知道一个不使用userAgent嗅探的解决方案。

答案 3 :(得分:0)

问题是浏览器默认为平移操作而不是触摸操作...请查看http://msdn.microsoft.com/en-us/library/ie/dn265022(v=vs.85).aspx以获取有关如何在css中控制默认操作的信息。