How can I stop text from being selected?

后端 未结 2 2020
旧时难觅i
旧时难觅i 2021-02-07 21:50

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

S

2条回答
  •  忘了有多久
    2021-02-07 22:20

    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'
    });
    

提交回复
热议问题