【发布时间】:2012-07-27 19:21:01
【问题描述】:
当我在 gridview 中按 shift 键选择一堆行时,默认文本选择(深蓝色)与我的自定义行选择颜色混合。
如何禁用 Web 应用程序中的默认文本选择。
【问题讨论】:
标签: javascript asp.net web-applications internet-explorer-6 textselection
当我在 gridview 中按 shift 键选择一堆行时,默认文本选择(深蓝色)与我的自定义行选择颜色混合。
如何禁用 Web 应用程序中的默认文本选择。
【问题讨论】:
标签: javascript asp.net web-applications internet-explorer-6 textselection
这对我来说可以在 FF 和 IE 中禁用“选择”:
// jQuery Solution
$(document).mousedown(function (e)
{
return false;
});
$(document).bind("selectstart", function (e)
{
return false;
});
如果你不使用 jQuery,这里是简单的 javascript 解决方案。
// Vanilla Javascript Solution
function attachEvent(element, eventName, handler)
{
if(element.addEventListener)
{
element.addEventListener(eventName, handler, false);
}
else
{
element.attachEvent("on" + eventName, handler);
}
}
attachEvent(document, "mousedown", function (e)
{
if(window.addEventListener)
{
e.preventDefault();
}
return false;
});
attachEvent(document, "selectstart", function (e)
{
return false;
});
【讨论】:
document.body.style.MozUserSelect = "none"; 禁用选择。
当您单击某处然后 shift+mousedown 其他地方时会发生浏览器选择,因此您需要取消第二次 mousedown。为此,您需要添加
return false;
到您的 onmousedown 事件处理程序。
【讨论】:
你的意思是你的文字被选中了?
您必须在选择过程中禁用文本选择,see here how to do it。
【讨论】: