回调函数采用单个参数;调用处理程序时,JavaScript事件对象将通过它传递。
事件对象通常是不必要的,并且会省略参数,因为当绑定处理程序时,通常会提供足够的上下文,以便确切地知道触发处理程序时需要执行的操作,但是有些属性需要访问。
让我们看一个isDefaultPrevented()方法的例子。该isDefaultPrevented()方法检查是否曾经在此事件对象上调用过event.preventDefault()。
您可以尝试运行以下代码来学习如何在jQuery中使用偶数对象:
<html>
<head>
<title>jQuery isDefaultPrevented() method</title>
<script src = "https://cdn.staticfile.org/jquery/2.1.3/jquery.min.js"></script>
<script>
$(document).ready(function() {
$("a").click(function(event){
if ( event.isDefaultPrevented() ){
alert( "Default behavior is disabled - 1" );
}else{
alert( "Default behavior is enabled - 1" );
}
event.preventDefault();
if ( event.isDefaultPrevented() ){
alert( "Default behavior is disabled - 2" );
}else{
alert( "Default behavior is enabled - 2" );
}
});
});
</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>