jQuery或jQuery- ui是否有任何功能禁用给定文档元素的文本选择?


当前回答

这其实很简单。 要禁用文本选择(也可以点击+拖动文本(例如Chrome中的链接)),只需使用以下jQuery代码:

$('body, html').mousedown(function(event) {
    event.preventDefault();
});

这一切都是为了防止在主体和html标记中单击鼠标(mousedown())时发生默认情况。你可以很容易地通过改变两个引号之间的文本来改变元素(例如改变$('body, html')到$('#myUnselectableDiv'),使myUnselectableDiv成为,好吧,不可选择的div。

一个简短的片段向你展示/证明这一点:

$ (' # no-select ') .mousedown(函数(事件){ event.preventDefault (); }); < script src = " https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js " > < /脚本> <span id="no-select">我打赌你不能选择这个文本,或拖动<a href="#">这个链接</a>, </span> <br/><span>但是你可以选择这个文本,并拖动<a href="#">this link</a>!< / span >

请注意,这个效果不是完美的,并且在整个窗口不可选的情况下表现最好。你可能还想补充

取消一些热键(如Ctrl+a和Ctrl+c)。测试:Cmd+a和Cmd+c

也可以用上面弗拉基米尔的回答。(点击这里查看他的职位)

其他回答

这里有一个更全面的解决方案来断开选择,并取消一些热键(如Ctrl+a和Ctrl+c)。测试:Cmd+a和Cmd+c

(function($){

  $.fn.ctrlCmd = function(key) {

    var allowDefault = true;

    if (!$.isArray(key)) {
       key = [key];
    }

    return this.keydown(function(e) {
        for (var i = 0, l = key.length; i < l; i++) {
            if(e.keyCode === key[i].toUpperCase().charCodeAt(0) && e.metaKey) {
                allowDefault = false;
            }
        };
        return allowDefault;
    });
};


$.fn.disableSelection = function() {

    this.ctrlCmd(['a', 'c']);

    return this.attr('unselectable', 'on')
               .css({'-moz-user-select':'-moz-none',
                     '-moz-user-select':'none',
                     '-o-user-select':'none',
                     '-khtml-user-select':'none',
                     '-webkit-user-select':'none',
                     '-ms-user-select':'none',
                     'user-select':'none'})
               .bind('selectstart', false);
};

})(jQuery);

并调用示例:

$(':not(input,select,textarea)').disableSelection();

jsfiddle.net/JBxnQ/

这对于旧版本的FireFox来说可能也不够(我不知道是哪个)。如果所有这些都不起作用,请添加以下内容:

.on('mousedown', false)

这可以很容易地完成使用JavaScript这是适用于所有浏览器

<script type="text/javascript">

/***********************************************
* Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

function disableSelection(target){
if (typeof target.onselectstart!="undefined") //For IE 
    target.onselectstart=function(){return false}
else if (typeof target.style.MozUserSelect!="undefined") //For Firefox
    target.style.MozUserSelect="none"
else //All other route (For Opera)
    target.onmousedown=function(){return false}
target.style.cursor = "default"
}
 </script>

调用此函数

<script type="text/javascript">
   disableSelection(document.body)
</script>

最好和最简单的方法,我发现它,防止ctrl + c,右键单击。在本例中,我屏蔽了所有内容,因此不需要指定任何内容。

$(document).bind("contextmenu cut copy",function(e){
    e.preventDefault();
    //alert('Copying is not allowed');
});

这其实很简单。 要禁用文本选择(也可以点击+拖动文本(例如Chrome中的链接)),只需使用以下jQuery代码:

$('body, html').mousedown(function(event) {
    event.preventDefault();
});

这一切都是为了防止在主体和html标记中单击鼠标(mousedown())时发生默认情况。你可以很容易地通过改变两个引号之间的文本来改变元素(例如改变$('body, html')到$('#myUnselectableDiv'),使myUnselectableDiv成为,好吧,不可选择的div。

一个简短的片段向你展示/证明这一点:

$ (' # no-select ') .mousedown(函数(事件){ event.preventDefault (); }); < script src = " https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js " > < /脚本> <span id="no-select">我打赌你不能选择这个文本,或拖动<a href="#">这个链接</a>, </span> <br/><span>但是你可以选择这个文本,并拖动<a href="#">this link</a>!< / span >

请注意,这个效果不是完美的,并且在整个窗口不可选的情况下表现最好。你可能还想补充

取消一些热键(如Ctrl+a和Ctrl+c)。测试:Cmd+a和Cmd+c

也可以用上面弗拉基米尔的回答。(点击这里查看他的职位)

如果你使用jQuery UI,有一个方法,但它只能处理鼠标选择(即CTRL+ a仍然有效):

$('.your-element').disableSelection(); // deprecated in jQuery UI 1.9

代码非常简单,如果你不想使用jQuery UI:

$(el).attr('unselectable','on')
     .css({'-moz-user-select':'-moz-none',
           '-moz-user-select':'none',
           '-o-user-select':'none',
           '-khtml-user-select':'none', /* you could also put this in a class */
           '-webkit-user-select':'none',/* and add the CSS class here instead */
           '-ms-user-select':'none',
           'user-select':'none'
     }).bind('selectstart', function(){ return false; });