JavaScript 阻止事件的默认行为
1、右键点击
document.oncontextmenu = function(){
return false;
}
2、a链接默认行为
<!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> <script> window.onload = function() { var aLink = document.getElementById('a1'); /* aLink.onclick = function() { return false; } */ aLink.onclick = function(ev) { var e = ev || window.event; preDefault(e); console.log('asdf'); } // e.preventDefault; 阻止默认行为,可以放在任何地方 W3C // window.event.returnValue = false; 兼容ie浏览器 function preDefault(e) { if (e.preventDefault) { e.preventDefault(); } else { window.event.returnValue = false; } } } </script> </head> <body> <a id="a1" href="https://www.baidu.com">百度</a> </body> </html>