我正在事件处理程序中处理两个事件,两者都是focusout和。
作为事件处理程序中的最后一行
调用似乎部分工作 -  工作正常,但正在触发两次。
keydown$(elementID).on()$(elementID).off("focusout keydown");.on()focusoutkeydown
编辑:随着
@Barmar的发现,首先keydown触发focusout然后. 这发生在 Firefox 22 中,但显然不在 Chrome 29 中。 keydown
这是HTML:
<input type = "text" id = "textField" />
<input type = "button" onclick = "setFocus();" value = "click here" />
<ol>
      <li>Type some text into the textfield.</li>
      <li>Click the button.</li>
      <li>
            Click out of the textfield, or
            <br />
            <i>press enter for some weird behavior.</i>
      </li>
</ol>
...这是javascript / jQuery:
 function setFocus() {
       $("#textField").focus();
       $("#textField").select();
       var count = 1;
       $("#textField").on("focusout keydown", function(e) {
              // if clicked away, or the enter key is pressed:
              if (e.type == "focusout" || e.keyCode == 13) {
                     alert(e.type + ": " + count++);
              }
              // this doesn't seem to be working correctly:
              $("#textField").off("focusout keydown");
       });
 }
...这是jsFiddle。