javascript Jquery - 拖动时禁用选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4975222/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:32:30  来源:igfitidea点击:

Jquery - disable select when dragging

javascriptjquerydom

提问by John

I'm trying to do my own drag and drop function using jquery library. But everytime I mousedown on an image, then mousemove, my browser "highlights" or "selects" the image, which disrupts my mousemove operation.

我正在尝试使用 jquery 库来做我自己的拖放功能。但是每次我在图像上按下鼠标,然后鼠标移动时,我的浏览器都会“突出显示”或“选择”该图像,这会破坏我的鼠标移动操作。

How do I disable the select/highlight? I tried $('img').onselectstart = function() {return false;}, but that didn't work.

如何禁用选择/突出显示?我试过了$('img').onselectstart = function() {return false;},但这没有用。

回答by alex

You could prevent the default behaviour of the dragstartevent...

您可以阻止dragstart事件的默认行为...

$('img').bind('dragstart', function(event) { event.preventDefault(); });

jsFiddle.

js小提琴

回答by claviska

jQuery UI has an undocumented method that it uses to disable browser text selection. You can call it using this syntax:

jQuery UI 有一个未公开的方法,用于禁用浏览器文本选择。您可以使用以下语法调用它:

$('IMG').disableSelection();

Remember that you need to be using jQuery UI (which I assume you are).

请记住,您需要使用 jQuery UI(我假设您是)。