preventDefault()方法可防止浏览器执行默认操作。
您可以使用方法 isDefaultPrevented 来了解是否曾经(在该事件对象上)调用过此方法。
event.preventDefault()
以下是一个简单的示例,简单说明了此方法的用法。此示例演示如何阻止浏览器将页面更改为任何锚点的href。
<html> <head> <title>The jQuery Example</title> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"> </script> <script type="text/javascript" language="javascript"> $(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>
这将产生以下输出-
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)