我试图通过以下考虑来防止在输入字段上进行选择
- 防止使用鼠标选择
- 防止使用键盘进行选择(包括 Ctrl+A、shift+箭头)
- 允许使用键盘和鼠标聚焦到现场
到目前为止,我已经尝试过这些东西:
CSS
我尝试使用以下课程
.unselectable {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
结果:仍然可以选择
我也尝试过以下但没有成功:
$("#my_field").attr('unselectable','on')
.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; });
Javascript
我试过下面
$( "#my_field" ).select(function(e) {
console.log("Select called");
e.preventDefault();
});
结果:控制台打印了消息,但是选择仍然有效
谢谢你的帮助
原文由 user2636664 发布,翻译遵循 CC BY-SA 4.0 许可协议
可以通过将元素的
selectionStart
设置为selectionEnd
onselect
事件来完成: