Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.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 与disableSelection函数相反,用于为特定HTML元素启用文本选择_Javascript_Text_Selection - Fatal编程技术网

Javascript 与disableSelection函数相反,用于为特定HTML元素启用文本选择

Javascript 与disableSelection函数相反,用于为特定HTML元素启用文本选择,javascript,text,selection,Javascript,Text,Selection,我正在使用一个名为disableSelection的JavaScript函数来防止对特定元素进行文本选择。此函数的声明如下: function disableSelection(target) { if (typeof target.onselectstart!="undefined") //IE route target.onselectstart=function(){return false} else if (typeof target.style.Moz

我正在使用一个名为disableSelection的JavaScript函数来防止对特定元素进行文本选择。此函数的声明如下:

function disableSelection(target)
{
    if (typeof target.onselectstart!="undefined") //IE route
        target.onselectstart=function(){return false}
    else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
        target.style.MozUserSelect="none"
    else //All other route (ie: Opera)
        target.onmousedown=function(){return false}
    target.style.cursor = "default"
}
我想在整个页面上禁用文本选择,表单元素除外。如果我调用disableSelectiondocument.body,它将完成这项工作,但它也将禁用表单元素上的文本选择,但这仅在Firefox上发生

我的问题是如何防止表单字段受此文本禁用器脚本的影响?我可以标记除表单字段之外的所有内容,但这需要很多努力

我会感谢你在这方面的帮助


注意:我从中找到了disableSelection脚本。

通过从事件返回false,您将禁用浏览器选择文本的默认行为。因此,我会像这样修改函数

function disableSelection(target, enable)
{
    if (typeof target.onselectstart!="undefined") //IE route
        target.onselectstart=function(){return enable}
    else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
        target.style.MozUserSelect=enable?"all":"none";
    else //All other route (ie: Opera)
        target.onmousedown=function(){return enable}
    target.style.cursor = "default"
}
然后把这个叫做你的东西

disableSelection(document.body, false);
disableSelection(document.forms[0], true);

如果work没有通过从事件返回false来测试它,您将禁用浏览器选择文本的默认行为。因此,我会像这样修改函数

function disableSelection(target, enable)
{
    if (typeof target.onselectstart!="undefined") //IE route
        target.onselectstart=function(){return enable}
    else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
        target.style.MozUserSelect=enable?"all":"none";
    else //All other route (ie: Opera)
        target.onmousedown=function(){return enable}
    target.style.cursor = "default"
}
然后把这个叫做你的东西

disableSelection(document.body, false);
disableSelection(document.forms[0], true);

应该工作没有测试它

脚本必须在标记下。如果只想禁用div:disableSelectiondocument.getElementById'divName'

脚本必须位于标记下。如果您只想禁用div:disableSelectiondocument.getElementById'divName'

这对我有效,仅在chrome中测试:

function enableSelection(target) {
    //For IE This code will work
    if(typeof target.onselectstart != "undefined") {
        target.onselectstart = function() {
            return true;
        }
    }

    //For Firefox This code will work
    else if(typeof target.style.MozUserSelect != "undefined") {
        target.style.MozUserSelect = "all";
    }

    //All other  (ie: Opera) This code will work
    else {
        target.onmousedown = function() {
            return true;
        }
        target.style.cursor = "default";
    }
}

这适用于我,仅在chrome中测试:

function enableSelection(target) {
    //For IE This code will work
    if(typeof target.onselectstart != "undefined") {
        target.onselectstart = function() {
            return true;
        }
    }

    //For Firefox This code will work
    else if(typeof target.style.MozUserSelect != "undefined") {
        target.style.MozUserSelect = "all";
    }

    //All other  (ie: Opera) This code will work
    else {
        target.onmousedown = function() {
            return true;
        }
        target.style.cursor = "default";
    }
}

没用,但我会再试一次。如果我成功了,我将在这里发布。为了测试它是否有效,我调用了disableSelectiondocument.body,false;我把disableSelectiondocument.body称为true;就在那之后。但在整个页面上未启用选择。有什么想法吗?我用Firefox 9.0.1进行了测试。注意:即使我使用原始脚本,其他浏览器也不会出现原始问题。它不起作用,但我会尝试更多。如果我成功了,我将在这里发布。为了测试它是否有效,我调用了disableSelectiondocument.body,false;我把disableSelectiondocument.body称为true;就在那之后。但在整个页面上未启用选择。有什么想法吗?我用Firefox 9.0.1进行了测试。注意:即使使用原始脚本,其他浏览器也不会出现原始问题。