如何处理按下 ENTER 键以触发 onBlur() 事件?

mon*_*r_h 5 javascript ipad

<form>我有一个 iPad 网络应用程序,有时会很大。其中的每个输入都具有控制 keyUp 和 Blur 事件上的值的函数。

问题是,如果用户在键入时意外点击“GO”按钮(被视为“输入”按键),则会提交表单。我想拦截此行为并触发焦点元素的 onBlur() 事件。

现在我有这个:

load(){
  document.addEventListener("keydown",logPressedKeys,false);
}
/**
 * logs the keys hit in the console, will eventually trigger my onBlur event
 */
  function logPressedKeys(e) {
      console.log(e.keyCode);
      if (e.keyCode==13) {
      console.log('Enter spotted: prevent!');
      e.preventDefault();//Shall prevent submitting
      return false;//Hoping it prevents default if above fails
  }
}
Run Code Online (Sandbox Code Playgroud)

你们对此有什么建议/想法/改进吗?

注意:必须至少有一个输入集中,iPad 的键盘才会弹出。

mon*_*r_h 2

发现 document.activeElement 在 iPad 的 Safari 中有效。

function logPressedKeys(e) {
    console.log(e.keyCode);
    if (e.keyCode==13) {
      e.preventDefault();
      console.log('Enter spotted: prevent!');
      temp=document.activeElement;
      //console.log(temp);
      temp.onblur();
      return false;
}
return true;
}
Run Code Online (Sandbox Code Playgroud)