isDefaultPrevented()方法是jQuery中的内置方法, 它检查是否为事件调用了preventDefault()方法。此方法返回一个布尔值。如果在事件上调用了preventDefault(), 则返回True, 否则返回False。
语法如下:
event.isDefaultPrevented()
参数:它接受一个参数事件它来自事件绑定功能。
返回值:如果在事件上调用了preventDefault()函数, 则返回True, 否则返回false。
示例1:此示例check isPreventDefault()是否在事件上调用方法。
<!doctype html>
< html >
< head >
< title >
isPreventDefault() Method
</ title >
< script src =
"https://code.jquery.com/jquery-3.3.1.min.js" >
</ script >
</ head >
< body >
< a href = "https://www.lsbin.org" >
Go to Homepage
</ a >
< div id = "initial" ></ div >
< div id = "prevented" ></ div >
< div id = "response" ></ div >
<!-- Script to check preventDefault() Method called or not -->
< script >
$( "a" ).click(function( event ) {
$( "#initial" ).html( "Before: isDefaultPrevented? < strong >"
+event.isDefaultPrevented()+"</ strong >");
event.preventDefault();
$( "#prevented" ).html( "preventDefault() is called now.");
$( "#response" ).html( "So, you are not going anywhere."
+ " isDefaultPrevented? < strong >"
+ event.isDefaultPrevented() + "</ strong >");
});
</ script >
</ body >
</ html >
输出如下:
之前单击链接:
单击链接后:
注意:粗体字(真假)是...的价值isDefaultPrevented()方法。
范例2:此示例检查isDefaultPrevented()方法是否阻止默认操作。
<!doctype html>
< html >
< head >
< title >
isPreventDefault() Method
</ title >
< script src =
"https://code.jquery.com/jquery-3.3.1.min.js" >
</ script >
</ head >
< body >
< form action = "action.php" >
Input:< br >
< input type = "text" name = "input_1" >
< button type = "submit" >Submit</ button >
</ form >
<!-- Script to describe isDefaultPrevented() Method -->
< script >
$( "button" ).click(function( event ) {
if(event.isDefaultPrevented())
alert('Default action was prevented');
else
alert('Click Ok');
event.preventDefault();
if(event.isDefaultPrevented())
alert('Default action was prevented');
else
alert('Click Ok');
});
</ script >
</ body >
</ html >
输出如下:
之前单击提交按钮:
单击提交按钮后:
完成上述步骤后, 将阻止默认事件: