問題描述
這是一個有點具體的問題,所以我會直奔主題.
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模板網!