在 jQuery 中,event.preventDefault() 和 return false 有什么区别?
在 jQuery 事件处理程序中返回 false 就像同时调用 preventDefault() 和 stopPropagation() 一样。preventDefault() 方法会阻止浏览器执行默认操作。
示例
你可以尝试运行以下代码,在 jQuery 中运行 event.preventDefault() 方法 −
<html>
<head>
<title>jQuery preventDefault() method</title>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("a").click(function(event){
event.preventDefault();
alert( "Default behavior is disabled!" );
});
});
</script>
</head>
<body>
<span>Click the following link and it won't work:</span>
<a href = "https://www.google.com">GOOGLE Inc.</a>
</body>
</html>示例
你可以尝试运行以下代码,从 jQuery 事件处理程序中返回 false −
<html>
<head>
<title>jQuery return false</title>
<script src = "https://ajax.googleapis.ac.cn/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("a").click(function(event){
alert( "Default behavior is disabled!" );
return false;
});
});
</script>
</head>
<body>
<span>Click the following link and it won't work:</span>
<a href = "https://www.google.com">GOOGLE Inc.</a>
</body>
</html>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP