为函数添加一个参数,这个参数保留着这个对象。而参数的.target属性保留着事件发生的目标元素。
方法一:确定目标对象
.target
<script type="text/javascript"> $(function(){ $('.box a').click(function(event) { alert('a'); }) $('.box').click(function(){ if(event.target==this) { alert('box'); } }) }) </script>
方法二:防止事件传播
.stopPropagation()
<script type="text/javascript"> $(function(){ $('.box a').click(function(event) { alert('a'); event.stopPropagation(); }) $('.box').click(function(){ alert('box'); }) }) </script>