gpt4 book ai didi

javascript - event.dataTransfer.setData 中的冲突

转载 作者:行者123 更新时间:2023-11-28 03:08:28 28 4
gpt4 key购买 nike

event.dataTransfer.setData中的数据类型在firefox和IE中冲突。IE不支持'text/html'。所以我应该使用'text'。问题是firefox不支持'text'。它单独支持“text/html”或“html”。有解决此问题的解决方案吗?

html:

    <div class="box" draggable="true">
<img src="drag icon.png" width="16" height="16"/>
<header>B</header>
<p>
Put me
</p>
</div>
<div class="box" draggable="true">
<img src="drag icon.png" width="16" height="16"/>
<header>C</header>
<p>
right
</p>
</div>
<div class="box" draggable="true">
<img src="drag icon.png" width="16" height="16"/>
<header>D</header>
<p>
into
</p>
</div>
<div class="box" draggable="true">
<img src="drag icon.png" width="16" height="16"/>
<header>E</header>
<p>
the
</p>
</div>

脚本:

  (function () {
var id_ = 'boxes-example';
var boxes_ = document.querySelectorAll('#' + id_ + ' .box');
var dragSrcEl_ = null;

this.handleDragStart = function (e) {
e.dataTransfer.effectAllowed = 'move';
e.dataTransfer.setData('text/html', this.innerHTML);

dragSrcEl_ = this;

this.style.opacity = '0.5';

// this/e.target is the source node.
this.addClassName('moving');
};

this.handleDragOver = function (e) {
if (e.preventDefault) {
e.preventDefault(); // Allows us to drop.
}

e.dataTransfer.dropEffect = 'move';

return false;
};

this.handleDragEnter = function (e) {
this.addClassName('over');
};

this.handleDragLeave = function (e) {
// this/e.target is previous target element.

this.removeClassName('over');
};

this.handleDrop = function (e) {
// this/e.target is current target element.

if (e.stopPropagation) {
e.stopPropagation(); // stops the browser from redirecting.
}

// Don't do anything if we're dropping on the same box we're dragging.
if (dragSrcEl_ != this) {
dragSrcEl_.innerHTML = this.innerHTML;
this.innerHTML = e.dataTransfer.getData('text/html');
}

return false;
};

this.handleDragEnd = function (e) {
// this/e.target is the source node.
this.style.opacity = '1';

[ ].forEach.call(boxes_, function (box) {
box.removeClassName('over');
box.removeClassName('moving');
});
};

[ ].forEach.call(boxes_, function (box) {
box.setAttribute('draggable', 'true'); // Enable boxes to be draggable.
box.addEventListener('dragstart', this.handleDragStart, false);
box.addEventListener('dragenter', this.handleDragEnter, false);
box.addEventListener('dragover', this.handleDragOver, false);
box.addEventListener('dragleave', this.handleDragLeave, false);
box.addEventListener('drop', this.handleDrop, false);
box.addEventListener('dragend', this.handleDragEnd, false);
});
})();

这是一个示例基本的 html5 拖放代码。您可以使用任何拖放代码并检查此问题

最佳答案

尝试...

this.innerHTML = e.dataTransfer.getData('text') || e.dataTransfer.getData('text/html');

或者...

try {
this.innerHTML = e.dataTransfer.getData('text');
} catch (e) {
this.innerHTML = e.dataTransfer.getData('text/html');
}

如果 e.dataTransfer.getData 抛出异常...

关于javascript - event.dataTransfer.setData 中的冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31587824/

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