简单说下事件委托与阻止冒泡
html:
<ul class="clearfix" data-type="cityPick"> <li class="active_sort_opts" data-id="0">全部</li> <li data-id="88">纽约</li> <li data-id="119">洛杉矶</li> <li data-id="138">拉斯维加斯</li> <li data-id="84">夏威夷</li> <li data-id="120">旧金山</li> <li data-id="105">奥兰多</li> <li data-id="118">西雅图</li> </ul>
$("ul[data-type='cityPick']").on('click',function(){ alert("父元素ul被点击"); }); $("ul[data-type='cityPick']").on('click','li',function(){ alert("子元素li被点击"); });
解决:
$("ul[data-type='cityPick']").on('click',function(){ alert("父元素ul被点击"); }); $("ul[data-type='cityPick']").on('click','li',function(e){ e.stopPropagation();//阻止冒泡 alert("子元素li被点击"); });
更多讲解与优秀文章:http://www.ituring.com.cn/article/467#
原文地址:http://blog.csdn.net/smy_yu/article/details/38945765