jquery 事件对象属性小结

2020-05-24 21:21:25易采站长站整理


isDefaultPrevented()
是否调用过

 

preventDefault()

方法

$(“a”).click(function(event){
alert( event.isDefaultPrevented() );
event.preventDefault();
alert( event.isDefaultPrevented() );
});


stopPropagation()
取消事件冒泡
$(“p”).click(function(event){
event.stopPropagation();
// do something
});


isPropagationStopped()
是否调用过

 

stopPropagation()

方法

$(“p”).click(function(event){
alert( event.isPropagationStopped() );
event.stopPropagation();
alert( event.isPropagationStopped() );
});


stopImmediatePropagation()
取消执行其他的事件处理函数并取消事件冒泡.如果同一个事件绑定了多个事件处理函数, 在其中一个事件处理函数中调用此方法后将不会继续调用其他的事件处理函数.
$(“p”).click(function(event){
event.stopImmediatePropagation();
});
$(“p”).click(function(event){
// This function won’t be executed
});


isImmediatePropagationStopped()
是否调用过

 

stopImmediatePropagation()

方法

$(“p”).click(function(event){
alert( event.isImmediatePropagationStopped() );
event.stopImmediatePropagation();
alert( event.isImmediatePropagationStopped() );
});


这些函数中  stopPropagation()  是我们最长用的也是一定会用到的函数. 相当于操作原始event对象的event.cancelBubble=true来取消冒泡.