1、e.stopPropagation()
阻止任何父事件被执行(阻止事件冒泡)
冒泡:span -> p -> div
<div>
this is a div element;
<p>
this is a p element;
<span>this is a span element;</span>
</p>
</div>
<script>
$("span").click(function(e) {
e.stopPropagation();
alert('The span element was clicked;'); //只有点击span标签时执行
});
$("p").click(function(e) {
alert('The p element was clicked;'); //点击p标签时先执行
})
$("div").click(function(e) {
alert('The div element was clicked;'); //点击p和div标签时都执行
})
</script>
2、e.preventDefault()
阻止元素发生默认的行为
用法:
1、阻止打开a标签的URL链接
2、当点击提交按钮时,阻止表单提交
$("a").click(function(e){
e.preventDefault();
});
3、return false;
相当于执行了 e.preventDefault() + e.stopPropagation()