防止鼠标拖动时选择文本(Prevent text selection on mouse drag)

编程入门 行业动态 更新时间:2024-10-10 13:22:44
防止鼠标拖动时选择文本(Prevent text selection on mouse drag)

如何在使用传统方法e.stopPropagation 情况下防止文本选择(底部的相关问题)? 我可以打电话说“不重视任何突出显示的内容”吗?

我在文档上绑定了鼠标事件,因为页面中大约有一百个[或多或少]个元素,我想互相交互。

我没有将事件绑定到元素的原因是因为当鼠标离开元素时,所有的鼠标事件停止触发 。 我需要绑定到文档以正确观察鼠标事件。

使用传统方法(下面的链接)的问题在于,在侦听器被触发时,具有文本的元素已经评估了鼠标事件。

我的代码适用于Chrome。 以下问题适用于Firefox。

相关问题: 如何通过拖动光标来防止文本/元素选择 这涉及防止冒泡到文本元素:

if(e.stopPropagation) e.stopPropagation(); if(e.preventDefault) e.preventDefault(); e.cancelBubble=true; e.returnValue=false; return false;

How can I prevent text selection without using the traditional method e.stopPropagation (related question at bottom)? Is there a call I can make to say "unhighlight whatever is highlighted"?

I bound mouse events on the document because there are about a hundred [more or less] elements in the page that I want to have interact with each other.

The reason I don't bind events to the elements instead is because when the mouse moves off the element, all mouse events stop triggering. I need to bind to the document to correctly watch mouse events.

The problem with using the method traditional method (link below) is that by the time the listener is triggered, the elements with text has already evaluated the mouse event.

My code works fine for Chrome. The issues below applies to Firefox.

Related Question: How can I prevent text/element selection with cursor drag This talks about preventing bubbling to the text element with:

if(e.stopPropagation) e.stopPropagation(); if(e.preventDefault) e.preventDefault(); e.cancelBubble=true; e.returnValue=false; return false;

最满意答案

道歉删除我最后的答案,但我意识到它仍然不会在FF。

我相信这个答案以及上面的CSS答案会产生你正在寻找的结果。 这是一个跨浏览器的解决方案。

var unFocus = function () { if (document.selection) { document.selection.empty() } else { window.getSelection().removeAllRanges() } }

叫onmouseup和onmousemove做的伎俩。

这里是小提琴: http : //jsfiddle.net/z2kpcwb6/

Apologies for deleting my last answer but I realized it still wouldn't work in FF.

I believe that this answer, along with the CSS answer above, will produce the result you're looking for. Here is a solution that works cross-browser.

var unFocus = function () { if (document.selection) { document.selection.empty() } else { window.getSelection().removeAllRanges() } }

called onmouseup and onmousemove does the trick.

Here's the fiddle: http://jsfiddle.net/z2kpcwb6/

更多推荐

本文发布于:2023-08-08 00:24:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466667.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:鼠标   拖动   文本   Prevent   drag

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!