How can I stop text from being selected?

旧巷老猫 提交于 2019-12-09 05:29:19

问题


In my web app the user can sometimes click the same button a number of times, skipping through messages and things, causing the </a> to be selected.

So how can I prevent this using Javascript (jQuery)


回答1:


This solution worked for me: 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'
});



回答2:


You dont need script for this, here is the css:

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


来源:https://stackoverflow.com/questions/3469900/how-can-i-stop-text-from-being-selected

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!