如何使用JavaScript临时禁用文本选择? [英] How can I disable Text Selection temporarily using JavaScript?

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

问题描述

这是一个特定的问题,所以我可以看到这一点。

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或点击事件返回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浏览器中,您可以在上调用 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;
}

编辑:只需重新阅读该问题,您可能还想防止您的代码部分处理实际拖动的默认操作,而不仅仅是初始mousedown或点击。

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临时禁用文本选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆