gpt4 book ai didi

javascript - dragenter 在 html5 拖放中的 dragleave 之前调用两次

转载 作者:太空宇宙 更新时间:2023-11-04 07:31:51 26 4
gpt4 key购买 nike

我正在通过引用此 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>

提前致谢。

最佳答案

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

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

关于javascript - dragenter 在 html5 拖放中的 dragleave 之前调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49320348/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com