dragenter在html5拖放之前调用dragleave两次

时间:2018-03-16 12:04:28

标签: javascript jquery html5 css3

我正在通过引用这个link来研究HTML5拖放概念。目前,我面临dragenter事件的问题,该事件在dragleave之前第二次(对于子元素)被触发。因此,在dragenter {{}}}之后,我在dragleave之后删除的虚线边框样式在某些情况下没有删除。当我在谷歌搜索时,我提到了一些链接和link,但仍无法解决问题。我添加了CSS属性pointer-events: none但在IE9和IE10中不兼容。

请注意我需要兼容IE9及以上版本,Mozilla也是如此。

PFB代码段。

var dragSrcEl = null;
var dragEnteredSrcEl = null;
var collection = $();

function handleDragStart(ev) {
    dragSrcEl = ev.target;

    ev.target.style.opacity = 0.4;
    ev.target.classList.add('moving');

    ev.dataTransfer.setData('text/html', ev.target.innerHTML);
}

function handleDragOver(ev) {
    ev.preventDefault();
}

function handleDragEnter(ev, el) {
    ev.stopPropagation();
    console.log('drag enter!');
    var dragEnteringElement = ev.target;
    collection = collection.add(dragEnteringElement);
    // dragEnteredSrcEl = dragEnteringElement;
    el.classList.add('over');
}

function handleDrop(ev, el) {
    // console.log(ev.target);
    var droppingElement = el;
    // Don't do anything if dropping the same column we're dragging.
    if (dragSrcEl != ev.target) {
        // Set the source column's HTML to the HTML of the column we dropped on.
        dragSrcEl.style.opacity = 1;
        dragSrcEl.innerHTML = droppingElement.innerHTML;
        droppingElement.classList.remove('over');
        droppingElement.innerHTML = ev.dataTransfer.getData('text/html');
    }
}

function handleDragLeave(ev, el) {
    // console.log(ev.target);
    setTimeout(function() {
        var dragLeavingElement = ev.target;    
        console.log(collection.length);    
        collection = collection.not(dragLeavingElement);
        if (collection.length === 0) {
            console.log('drag leave!');
            el.classList.remove('over');
        }
    }, 1);
}

function handleDragEnd(ev) {
    ev.target.style.opacity = 1;
    ev.target.classList.remove('moving');
    ev.target.classList.remove('over');
}

function handleContentClick(content) {
    alert(content);
}
.clearfix {
    clear: both;
}

[draggable] {
    cursor: move;
}

.col-md-4 {
    width: 150px;
    height: 150px;
    border: 2px solid orange;
    float: left;
    margin-top: 5px;
    margin-right: 5px;
    text-align: center;
}

.col-md-4.over {
    border-style: dashed;
}

/* .col-md-4 h2 {
    pointer-events: none;
} */
<div class="container">
        <div class="col-md-4" draggable="true" ondragstart="handleDragStart(event)" ondrop="handleDrop(event, this)" ondragenter="handleDragEnter(event, this)" ondragover="handleDragOver(event)" ondragleave="handleDragLeave(event, this)" ondragend="handleDragEnd(event)">
            <h2 onclick="handleContentClick('Hello!')">Hello!</h2>
        </div>
        <div class="col-md-4" draggable="true" ondragstart="handleDragStart(event)" ondrop="handleDrop(event, this)" ondragenter="handleDragEnter(event, this)" ondragover="handleDragOver(event)" ondragleave="handleDragLeave(event, this)" ondragend="handleDragEnd(event)">
            <h2 onclick="handleContentClick('Welcome!')">Welcome!</h2>
        </div>
        <div class="col-md-4" draggable="true" ondragstart="handleDragStart(event)" ondrop="handleDrop(event, this)" ondragenter="handleDragEnter(event, this)" ondragover="handleDragOver(event)" ondragleave="handleDragLeave(event, this)" ondragend="handleDragEnd(event)">
            <h2 onclick="handleContentClick('World!')">World!</h2>
        </div>
        <div class="clearfix"></div>
        <div class="col-md-4" draggable="true" ondragstart="handleDragStart(event)" ondrop="handleDrop(event, this)" ondragenter="handleDragEnter(event, this)" ondragover="handleDragOver(event)" ondragleave="handleDragLeave(event, this)" ondragend="handleDragEnd(event)">
            <h2 onclick="handleContentClick('Big Hello!')">Big Hello!</h2>
        </div>
        <div class="col-md-4" draggable="true" ondragstart="handleDragStart(event)" ondrop="handleDrop(event, this)" ondragenter="handleDragEnter(event, this)" ondragover="handleDragOver(event)" ondragleave="handleDragLeave(event, this)" ondragend="handleDragEnd(event)">
            <h2 onclick="handleContentClick('Big Welcome!')">Big Welcome!</h2>
        </div>
        <div class="col-md-4" draggable="true" ondragstart="handleDragStart(event)" ondrop="handleDrop(event, this)" ondragenter="handleDragEnter(event, this)" ondragover="handleDragOver(event)" ondragleave="handleDragLeave(event, this)" ondragend="handleDragEnd(event)">
            <h2 onclick="handleContentClick('Big World!')">Big World!</h2>
        </div>
    </div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

提前致谢。

2 个答案:

答案 0 :(得分:0)

您应该使用event.stopPropagation();来防止事件与子元素一起触发。

有文档:https://api.jquery.com/event.stoppropagation/

答案 1 :(得分:0)

它是Firefox错误。在Firefox中,“ Dragenter-Event”被触发了两次。无论是否有子元素。 您可以尝试将event.target和event.currentTarget与指针事件结合使用:无。