我有一个示例代码,用于使用 j Query 在表单上禁用选择文本。它适用于 Firefox、Chrome 和 IE10。但我想允许在输入框中选择文本。它适用于 Firefox、chrome 但 IE 不允许在输入框中选择文本。我如何允许在输入框中选择文本值。
注意:我在 IE10 上测试过。
此致,
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>jQuery Disable Select Text - Example</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script>
(function($){
$(document).ready(function () {
$(document).bind("contextmenu", function (e) {
return false;
});
});
})(jQuery);
</script>
<script type="text/javascript">
(function($){
$.fn.ctrl = function (key, callback) {
if (typeof key != 'object') key = [key];
callback = callback || function () { return false; }
return $(this).keydown(function (e)
{
var ret = true;
$.each(key, function (i, k) {
if (e.keyCode == k.toUpperCase().charCodeAt(0) && e.ctrlKey) {
ret = callback(e);}
});
return ret;
});
};
$.fn.disableSelection = function() {
this.ctrl(['a','s','c']);
return this.attr('unselectable', 'off')
.css({'-moz-user-select':'-moz-none',
'-moz-user-select':'none',
'-o-user-select':'none',
'-khtml-user-select':'none',
'-webkit-user-select':'none',
'-ms-user-select':'none',
'user-select':'none'})
.bind('selectstart', function(){ return false; });
};
})(jQuery);
$(':not(input,select,textarea)').disableSelection();
</script>
</head>
<body>
<form id="_frm">
<div>
<p >Try selecting me with <span style="color:red">your mouse</span> or <span style="color:red">Ctrl+A</span></p>
First name: <input type="text" name="fname"><br>
Last name: <input type="text" name="lname"><br>
<input type="submit" value="Submit">
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<ol>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ol>
<ul>
<li>Coffee</li>
<li>Tea</li>
<li>Milk</li>
</ul>
</div>
</form>
</body>
</html>