Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 禁用除输入外的文本选择_Javascript_Jquery - Fatal编程技术网

Javascript 禁用除输入外的文本选择

Javascript 禁用除输入外的文本选择,javascript,jquery,Javascript,Jquery,我有一段代码来禁用所有文本选择。如何禁用除输入之外的所有文本?我尝试了$('*:not(input)')。disableTextSelect()但它禁用了对所有内容的选择(包括输入) 问题似乎是这种禁用是遗传的。因此,即使您没有在$()中选择它们,它们仍然会被禁用。但这也可能对我们有利 禁用后,可以启用输入 $('body').css('MozUserSelect', '-moz-none'); $('input').css('MozUserSelect', 'text'); 注意:该值必须为

我有一段代码来禁用所有文本选择。如何禁用除输入之外的所有文本?我尝试了
$('*:not(input)')。disableTextSelect()但它禁用了对所有内容的选择(包括输入)


问题似乎是这种禁用是遗传的。因此,即使您没有在$()中选择它们,它们仍然会被禁用。但这也可能对我们有利

禁用后,可以启用输入

$('body').css('MozUserSelect', '-moz-none');
$('input').css('MozUserSelect', 'text');
注意:该值必须为“-moz none”。如果“无”,则无法更改


我不能测试IE,也没有Opera的解决方案。但这可能会有所帮助。

这在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; }; ;
    }

感谢@user2873592,他提到在此处添加
html
将解决chrome滚动条无法拖动的问题。

您确定*是必要的吗?请尝试$(“:not(input)”)注意,禁用选择会更改浏览器的默认行为,并会混淆甚至烦扰用户。适用于IE、FF、Opera和Chromejust的IE、FF、Opera和Chromejust将其粘贴到文档中。准备好了,效果很好!!非常感谢@deerchao
    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; }; ;
    }
$(document).bind('mousedown selectstart', function(e) {
    return $(e.target).is('input, textarea, select, option, html');
});