2010-08-12 61 views
11

在我的網絡應用程序中,用戶有時可以多次點擊相同的按鈕,跳過消息和事物,導致選擇</a>如何停止選擇文本?

那麼,如何防止這種使用JavaScript(jQuery的)

+0

它會阻止他們繼續巡航郵件嗎?如果沒有,那麼我會建議不要,因爲它可能會阻止他們想要選擇文本。 – 2010-08-12 16:35:50

+0

聽起來不像我會喜歡它會影響他們的導航能力 - 只是意外選擇。馬雷克在他的答案中包含的鏈接允許僅在某些元素上禁用文本選擇。據推測,Ben只會在一個標籤上禁用選擇,這會阻止他的用戶。 – sblom 2010-08-12 16:47:33

回答

2

該解決方案爲我工作: http://chris-barr.com/entry/disable_text_selection_with_jquery/

$(function(){ 
    $.extend($.fn.disableTextSelect = function() { 
     return this.each(function(){ 
      if($.browser.mozilla){//Firefox 
       $(this).css('MozUserSelect','none'); 
      }else if($.browser.msie){//IE 
       $(this).bind('selectstart',function(){return false;}); 
      }else{//Opera, etc. 
       $(this).mousedown(function(){return false;}); 
      } 
     }); 
    }); 
    $('.noSelect').disableTextSelect();//No text selection on elements with a class of 'noSelect' 
}); 
+0

該網址似乎已轉移到:http://chris-barr.com/index.php/entry/disable_text_selection_with_jquery/ – Dwayne 2011-11-17 04:50:18

+0

此鏈接現在也被打破。 – 2011-12-05 23:15:02

+0

鏈接中的示例不適用於最新版本的Jquery。 $ .browser不再存在。 – 2013-07-04 20:44:24

29

你不需要這個腳本,這裏是CSS:

-webkit-touch-callout: none; 
-webkit-user-select: none; 
-khtml-user-select: none; 
-moz-user-select: none; 
-ms-user-select: none; 
user-select: none; 
+2

詢問的人可能已經預先決定要使用JS,但你的答案比任何腳本都要好。 +1 – mavrosxristoforos 2013-02-12 16:43:04

+1

適用於Chrome 25,但在IE10中不適用於我。 Ctrl + A選擇頁面上的所有文本。如果我雙擊文本,它將停止選擇文本,但是如果我從未使用此CSS的區域拖入使用此CSS的區域,它仍會突出顯示文本。 – 2013-07-04 20:41:40