<!DOCTYPE html>
<html lang="en">
<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>
<script>
function f1(){
alert("hello world");
return false;
};
</script>
</head>
<body>
<!-- 第一种方法 -->
<a href="http://www.baidu.com" onclick="return f1()">百度</a>
<!-- 第二种方法 -->
<a href="http://www.baidu.com" id="baidu">百度</a>
<script>
document.getElementById("baidu").onclick = function(){
alert("hello world");
return false;
}
</script>
</body>
</html>
<!-- 不管是哪种方法,只要在处理函数中返回的是return false就可以了 -->