event.stopPropagation()方法的使用

来源:互联网 发布:知返景行全文阅读19楼 编辑:程序博客网 时间:2024/05/22 15:15

event.stopPropagation()方法主要用于组织当前时间的冒泡,根据DOM事件的机制,在当前元素上触发的大多数事件都会冒泡传递到该元素的所有祖辈元素上,如果不在这些祖辈元素上相应的阻止事件,那么在祖辈元素上也会触发这些事件,通过event.stopPropagation()的阻止,事件则不会传播到任何的祖辈元素上去。例如:

$("p").click(function(event){ 
event.stopPropagation(); 
// do something 
});

注意其与event.cancelBubble的区别

<div onclick="alert('div')"> 
<input type="button" onclick="alert('input');event.cancelBubble=true" value="测试"> 
<input type="button" onclick="alert('input');" value="测试"> 
</div>

注意IE事件传递是从下往上的:

事件来源对象->上级对象->上上级对象->.....->body->document->window

0 0
原创粉丝点击