代码之家  ›  专栏  ›  技术社区  ›  Ben Shelock

如何阻止文本被选中?

  •  11
  • Ben Shelock  · 技术社区  · 16 年前

    在我的web应用程序中,用户有时可以多次单击同一按钮,跳过消息和内容,导致 </a>

    那么如何使用Javascript(jQuery)防止这种情况呢

    2 回复  |  直到 13 年前
        1
  •  2
  •   Popnoodles    10 年前

    这个解决方案对我有效: 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
  •  29
  •   Blowsie Mathias Bynens    13 年前

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