我使用以下脚本来禁用表格上脚本的突出显示...
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/