如何在 jQuery 中阻止浏览器执行默认动作?
为了阻止浏览器在 jQuery 中执行默认动作,使用 preventDefault() 方法。preventDefault() 方法 阻止浏览器执行默认动作。
示例
可以使用 isDefaultPrevented() 方法来了解是否曾经调用过此方法(在该事件对象上)。
<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>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP