禁用输入以外的文本选择

Mor*_*tar 4 javascript jquery

我有这段代码来禁用所有文本选择.除了输入外,我如何禁用所有文本?我试过 $('* :not(input)').disableTextSelect();但它禁用了一切的选择(包括输入)

$.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; });
                    }
                });
            });
            $('* :not(input)').disableTextSelect(); 
Run Code Online (Sandbox Code Playgroud)

dee*_*hao 14

$(document).bind('mousedown selectstart', function(e) {
    return $(e.target).is('input, textarea, select, option, html');
});
Run Code Online (Sandbox Code Playgroud)

感谢@ user2873592,谁提到html在这里添加会修复chrome滚动条无法拖动问题.


Sti*_*tif 6

这适用于IE和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; }; ;
    }
Run Code Online (Sandbox Code Playgroud)