I agree with meo's decision, but I will add some changes.
when we use document.onkeydown = fkey; for example, on the page we have another method that affected document.onkeydown, then the browser will only detect the last event. However, when we use: jQuery (document) .on ("KeyDown", FKEY); even if we have another function to handle the keydown event, all functions will be activated.
see the following example to understand:
var wasPressed = false; document.onkeydown = f1; document.onkeydown = f2; jQuery(document).on("keydown",f3); jQuery(document).on("keydown",f4); function f1(e){ e = e || window.event; if( wasPressed ) return; if (e.keyCode == 116) { alert("f5 pressed"); wasPressed = true; }else { alert("Window closed"); } } function f2(){ alert('f2'); } function f3(){ alert('f3'); } function f4(){ alert('f4'); }
what will be shown here: only 3 warnings: f2, f3 and f4. and in this example, the function f1 does not start.
senior
source share