disable text selection except input

前端 未结 3 1818
隐瞒了意图╮
隐瞒了意图╮ 2021-01-14 01:01

I\'ve got this snippet of code to disable all text selection. How would I go about disabling all text except for input? I tried $(\'* :not(input)\').disableTextSelec

相关标签:
3条回答
  • 2021-01-14 01:08
    $(document).bind('mousedown selectstart', function(e) {
        return $(e.target).is('input, textarea, select, option, html');
    });
    

    Thanks to @user2873592, who mentioned that adding html here would fix the chrome scroll bar can't be dragged issue.

    0 讨论(0)
  • 2021-01-14 01:20

    This works in IE and FF:

        jQuery(document).ready(function () {
            //Disable default text selection behavior
            toggleEnableSelectStart(false);
    
            //for inputs it must be possible to select text
            jQuery("input[type=text]").focusin(function () { toggleEnableSelectStart(true); });
            jQuery("input[type=text]").mouseover(function () { toggleEnableSelectStart(true); });
            jQuery("input[type=text]").focusout(function () { toggleEnableSelectStart(false); });
            jQuery("input[type=text]").mouseout(function () { toggleEnableSelectStart(false); });
    
        });
    
        function toggleEnableSelectStart(enable) {
            document.onmousedown = function (e) { return enable; };
            document.onselectstart = function (e) { return enable; }; ;
        }
    
    0 讨论(0)
  • 2021-01-14 01:30

    The problem seems to be that this disabling is inherited. So, even though you aren't selected them in the $() they still get disabled. But this can also be in our favor.

    After disabling, you can enable the inputs.

    $('body').css('MozUserSelect', '-moz-none');
    $('input').css('MozUserSelect', 'text');
    

    NOTE: the value must be '-moz-none'. If 'none', it can't be changed.

    I can't test IE nor do I have a solution for Opera. But maybe this will help part way.

    0 讨论(0)
提交回复
热议问题