如何使用 JavaScript 暂时禁用文本选择?

时间:2023-04-29
本文介绍了如何使用 JavaScript 暂时禁用文本选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

这是一个有点具体的问题,所以我会直奔主题.

this is a bit of a specific question so I'll get right to the point.

我正在为 Web 应用程序的一部分开发一个简短而简单的拖放例程,尽管拖放位工作正常,但该站点在操作过程中变得非常丑陋,因为浏览器仍然执行默认设置操作和文本选择.

I'm working on a short and simple drag and drop routine for part of a web application, and although the dragging and dropping bit works fine, the site goes all ugly-tastic during the operation because the browser still does the default operation and works on text selecting.

我尝试了一些解决方案,主要涉及从 mousedown 或 click 事件返回 false,虽然它们在某些浏览器中禁用了文本选择,但它们似乎也完全禁用了 mouseup 事件,破坏了脚本的drop"位并让这个永久浮动图标悬在鼠标周围——不好玩.

I've tried a few solutions mainly involving returning false from the mousedown or click events, and although they disable text selection in some browsers, they also seem to disable the mouseup event entirely, ruining the "drop" bit of the script and leaving this perma-floating icon hanging around the mouse-- not fun.

我真的不希望文本选择完全消失,我只是想建议浏览器...不要在拖动时这样做,如果这有意义的话.由于我知道受影响的区域(涉及 iframe),因此我可以轻松地将属性应用于受影响的元素等.如有必要,我可以发布代码,但我正在寻找更多通用解决方案.由于这只是美学问题,我正在寻找适用于大多数浏览器的修复程序,它并不是那么重要.

I don't really want text selection to be gone entirely, I just want to suggest that the browser... not do it while dragging, if that makes sense. Since I know the area that is affected (there are iframes involved) I can easily apply a property to the affected elements, etc. I can post code if necessary, but I'm looking for more of a general solution. Since this is an aesthetics thing only, I'm looking for a fix that works in most browsers, it's not really that crucial.

谢谢!

推荐答案

在 W3C 浏览器中,您可以在 event 对象上调用 preventDefault 方法.IE 等效项是将 returnValue 设置为 false.

In W3C browsers, you can call the preventDefault method on the event object. The IE equivalent is to set the returnValue to false.

function stopDefault(e) {
    if (e && e.preventDefault) {
        e.preventDefault();
    }
    else {
        window.event.returnValue = false;
    }
    return false;
}

只需重新阅读问题,您可能还希望在处理实际拖动的代码部分中阻止默认操作,而不仅仅是在初始鼠标按下或单击时.

Just re-read the question and you might also want to prevent the default action in the part of your code that handles the actual dragging, and not just at the initial mousedown or click.

这篇关于如何使用 JavaScript 暂时禁用文本选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

上一篇:变换上的 CSS3 动画:旋转.获取旋转元素当前度数的 下一篇:使用 HTML5 拖放防止拖动事件干扰 Firefox 中的输入

相关文章

最新文章