2015年11月23日 星期一

避免children control 的事件觸發parent的事件

今天寫了一個滑鼠移入與點擊時觸發的選單,裡面放了幾個按鈕,發現按鈕的mouseenter事件,會連帶觸發容器的mouseenter事件 找了一下發現可以用以下方式來避免 $(document).ready(function(){ $(".header").click(function(){ $(this).children(".children").toggle(); }); $(".header a").click(function(e) { e.stopPropagation(); }); });