Prevent drag event to interfere with input elements in Firefox using HTML5 drag/drop(使用 HTML5 拖放防止拖动事件干扰 Firefox 中的输入元素)
问题描述
似乎输入元素在放入具有可拖动=真"的元素时失去了很多功能.这似乎只发生在 Firefox 中.
It seems that an input element loses a lot of functionality when put into an element with draggable="true". This only seems to occur in firefox.
查看我的 jsfiddle:http://jsfiddle.net/WC9Fe/3/
See my jsfiddle: http://jsfiddle.net/WC9Fe/3/
HTML:
<div id="drag" draggable="true">
Drag this div <br />
<input id="message" type="text" />
</div>
<div id="drop">
Drop area
</div>
JS:
$('#drag').on('dragstart', function(e){
e.originalEvent.dataTransfer.setData('Text', $('#message').val());
e.originalEvent.dataTransfer.effectAllowed = 'move';
});
var drop = $('#drop');
drop.on('dragover', function(e){
e.preventDefault();
});
drop.on('dragenter', function(e){
e.preventDefault();
});
drop.on('drop', function(e){
alert('Target succesfully dropped: ' + e.originalEvent.dataTransfer.getData('Text'));
e.preventDefault();
});
现在尝试使用 firefox 在输入中选择文本.似乎不可能.在 IE/Chrome 中尝试相同的操作.似乎工作得很好.
Now try to select text in the input using firefox. Seems impossible. Try the same in IE/Chrome. Seems to work just fine.
推荐答案
据我所知,这是 FF 中的一个已知错误.一个快速(和dirty"的解决方法)是删除文本输入 focus
事件上的 draggable
属性,再次将其添加到文本输入 blur
事件,并禁用#drag div 上的文本选择,以便在您单击焦点输入外部时启用拖动(直接单击#div).
As far as I know this is a known bug in FF. A quick (and "dirty" workaround) would be to remove the draggable
attribute on text input focus
event, add it again on text input blur
event, and disable text selection on #drag div to enable dragging once you clicked outside the focused input (clicking on #div directly).
更新小提琴这里.
示例代码:
JS:
$('#message')
.on('focus', function(e) {
$(this).closest('#drag').attr("draggable", false);
})
.on('blur', function(e) {
$(this).closest('#drag').attr("draggable", true);
});
CSS:
.disable-selection {
/* event if these are not necessary, let's just add them */
-webkit-user-select: none;
-ms-user-select: none;
user-select: none;
/* this will add drag availability once you clicked the
#drag div while you're focusing #message div */
-moz-user-select: none;
}
希望对你有帮助.
这篇关于使用 HTML5 拖放防止拖动事件干扰 Firefox 中的输入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 HTML5 拖放防止拖动事件干扰 Firefox 中的输入


基础教程推荐
- 我什么时候应该在导入时使用方括号 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 响应更改 div 大小保持纵横比 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 动态更新多个选择框 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06