查资料后知道了 为什么点击b会弹出b再弹出a 这叫做js的冒泡触发,依次向外递归
话不多说上代码
<div style="width: 400px;height: 400px;background-color: #678678;font:3em bold;" id="outDiv">
out
<div style="width: 200px;height: 200px;margin: 50px;background-color: #eaceac;font:1em bold;" id="inDiv">in</div>
</div>
js
inDiv.addEventListener('click',function(e){
alert('in');
e.stopPropagation();//终止事件冒泡
},false);
outDiv.addEventListener('click',function(e){
alert('out');
},false);
或者是
$('#div1').click(function(e) {
alert('div1');
})
$('#div2').click(function(e) {
e.stopPropagation()
alert('div2');
//e.stopPropagation() 位置在上在下无所谓
})
e.stopPropagation()是阻止冒泡触发的功能()