禁用文本选择并添加例外

时间:2011-01-17 10:17:58

标签: javascript jquery input onmousedown onselect

如果我将此样式用于<body>

onmousedown='return false;' onselectstart='return false;'

是否可以通过使用javascript输入textareas并选择来提供异常? 如果是,那么你可以告诉我怎么样?

2 个答案:

答案 0 :(得分:2)

实际上,您可以禁用文本选择,而无需繁琐的JavaScript事件处理程序。请在此处查看我的回答:How to disable text selection highlighting using CSS?

摘要:在IE中使用CSS和unselectable expando。

CSS:

*.unselectable {
   -moz-user-select: -moz-none;
   -khtml-user-select: none;
   -webkit-user-select: none;
   -o-user-select: none;
   user-select: none;
}

HTML:

<div id="foo" unselectable="on" class="unselectable">...</div>

答案 1 :(得分:0)

您需要阻止event bubbling文本框并选择。

您可以使用:input选择器选择所有输入,文本区域,选择和按钮元素。

这样的东西
$(":input").bind("mousedown", function(e){
    e.stopPropagation();
});

查看working demo