javascript - 如何重新启用 ie 和 firefox 中选择元素的功能?

标签 javascript jquery

我使用以下脚本来禁用表格上脚本的突出显示...

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";
}

我希望能够反转上述方法的效果,以便当用户单击 td 中的输入时,他们可以使用 jQuery.select() 方法选择所有文本。

当我尝试时,我真的不明白如何扭转效果......

function enableSelection(target) {
    if (typeof target.onselectstart != "undefined") //IE route
        target.onselectstart = function () { return true; };
    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";
}

它不起作用,我在 IE 中遇到找不到对象的错误。希望有人可以帮助这是我周末必须完成的一项重要工作。

最佳答案

我能够让它在 IE 中工作。您确定这与其他错误无关吗?

这是一个在 IE 9 中适合我的测试页面:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body> 
    <div id="test">
        test test
        test test
        <br />
        test test
    </div>
    <button onclick="disableSelection(document.getElementById('test'))" value="off">off</button>
    <button onclick="enableSelection(document.getElementById('test'))" value="on">on</button>

    <script type="text/javascript">
        function enableSelection(target) {
            if (typeof target.onselectstart != "undefined") //IE route
                target.onselectstart = function () { return true; };
            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";
        }

        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";
        }


    </script>
</body>
</html>

关于javascript - 如何重新启用 ie 和 firefox 中选择元素的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9361930/

相关文章:

javascript - Deferred 和 Promise 功能无法正常工作

javascript - 将 anchor id 和按钮 id 传递给 jquery

javascript - 如何在 JavaScript 中通过 id 和 class 选择元素?

javascript - 使用 jQuery 获得头等舱和最后一等舱

javascript - 自动滚动的实时行情(如 Twitter 的 "Top Tweets")

javascript - 我怎样才能让这段代码适用于所有按钮?

javascript - 如何在index.html或javascript中获取Gradle构建版本

javascript - backbone.js 数据链接

javascript - 如何删除使用 jQuery 动态创建的类

javascript - 如何使用JS变量作为ngresource参数名称?