它似乎在我的 safari iphone 中工作:
$("#first").on('keyup', function(e){
if(isCharacterKeyPress(e)){
$("#second").focus();
console.log(e);
}
});
function isCharacterKeyPress(evt) {
if (typeof evt.which == "undefined") {
// This is IE, which only fires keypress events for printable keys
return true;
} else if (typeof evt.which == "number" && evt.which > 0) {
// In other browsers except old versions of WebKit, evt.which is
// only greater than zero if the keypress is a printable key.
// We need to filter out backspace and ctrl/alt/meta key combinations
return !evt.ctrlKey && !evt.metaKey && !evt.altKey && evt.which != 8;
}
return false;
}
请检查:https://jsfiddle.net/9u9hb839/4/
已编辑:
为了防止检测到其他按键而不是字符,我更新了我的代码:
var keypressed = false;
$("#first").on('keyup', function(e){
console.log('keyup');
if(keypressed){
$("#second").focus();
}
keypressed = false;
});
$("#first").on('keypress', function(e){
console.log('keypress');
keypressed = isCharacterKeyPress(e);
});
function isCharacterKeyPress(evt) {
if (typeof evt.which == "undefined") {
// This is IE, which only fires keypress events for printable keys
return true;
} else if (typeof evt.which == "number" && evt.which > 0) {
// In other browsers except old versions of WebKit, evt.which is
// only greater than zero if the keypress is a printable key.
// We need to filter out backspace and ctrl/alt/meta key combinations
return !evt.ctrlKey && !evt.metaKey && !evt.altKey && evt.which != 8;
}
return false;
}
试试这个:https://jsfiddle.net/9u9hb839/9/
在移动设备(safari、chrome)和桌面设备(chrome、firefox)中测试