好吧,我有类似的代码可以防止键盘上的“全选”动作:
$(document).keydown(function(e){
// CTRL key
if ( e.which == '17' || e.which == '224' ){
window.isCtrlHold = true;
}
// A key
// Prevent from select all from a page ( ctrl + a )
if ( e.which == '65' && window.isCtrlHold ){
e.preventDefault();
}
});
从另一个地方调用的另一个脚本逃脱了阻止预览代码:
$('input').focus(function(){
window.inSearch = true;
});
$(document).keydown(function(e){
// A ( "ctrl + a" if focus within text input )
if ( e.which == '65' && window.isCtrlHold && window.inSearch ){
// some code that do defult action eg "e.doDefault();"
}
});
最后,我需要防止“ctrl+a”(全选)而焦点不在 input[type=text] 内,如果焦点在输入内,则允许全选。