<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<a href="http://www.baidu.com" target="_blank" rel="external nofollow" id="btn">baidu</a>
<script>
var oBtn = document.getElementById('btn');
oBtn.onclick = function(e){
// 获取事件的兼容性写法 e 标准浏览器 window.event ie浏览器
e = e || window.event;
console.log('hahaha');
// 阻止默认行为
// e.preventDefault(); //标准浏览器
// e.returnValue = false; //ie浏览器
return false;
}
</script>
</body>
</html>
鼠标点击a标签,控制台直接输出字符串“hahaha”,不会跳转到百度页面;其中e.preventDefault();适用于标准浏览器,e.returnValue = false;适用于ie浏览器,通常情况下,为保证兼容性,这两句都要写,简便写法:直接return false ;