<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <p>hello world</p> <input type="text"> <script> document.addEventListener("mousedown" , function( evt ){ var e = evt || event; // 判定事件源是否是input,如果不是input那么我们就阻止默认事件; var target = e.target || e.srcElement; if( target.nodeName !== "INPUT"){ e.preventDefault(); } })
//禁用鼠标右键 document.addEventListener("contextmenu" , function( evt ){ var e = evt || event; e.preventDefault(); }); document.addEventListener("keydown" , function( evt ){ var e = evt || event; var code = e.keyCode || e.which; //禁止F12 if( code === 123){ e.preventDefault(); } }) </script> </body> </html>
运用事件源监听