我试图在已经有点击事件的li里面的div上添加一个click事件.我不想在触发div click事件时触发li click事件.
这是我想要做的事情的jsfiddle. http://jsfiddle.net/2srUd/5/
当我点击.inside时,我不希望#outside点击.我假设我需要广告不要选择$(“.inside”)
<script> $(document).ready(function() { $("#outside").click(function() { alert("outside click"); }); $(".inside").click(function() { alert("inside click"); }); }); </script> <ul> <li id="outside" style="border:2px solid red; padding:20px;"> <div class="inside" style="border:2px solid blue; ">Click This Div. I want to accept the inside click but not the outside click.</div> </li> </ul>
解决方法
简单修复:
$(".inside").click(function(e) { e.stopPropagation(); alert("inside click"); });
http://api.jquery.com/event.stopPropagation/
Prevents the event from bubbling up the DOM tree,preventing any
parent handlers from being notified of the event.
基本上你不希望它触发父的事件处理程序,所以你停止传播.