DIV CSS 佈局教程網

搞懂 jQuery 中 mouseleave 與 mouseout 的差別:模仿下拉框效果
編輯:JQuery特效代碼     

在處理滑鼠移出事件時,mouseleave 與 mouseout 看似相同,實則有明顯差異,這個差異在製作下拉選單時尤其關鍵。

兩者的差別

  1. 不論滑鼠離開被選元素還是它的任何子元素,都會觸發 mouseout 事件。
  2. 只有在滑鼠離開被選元素本身時,才會觸發 mouseleave 事件。

模仿下拉框的實作

<div class="sel_box">
<input type="button" value="請選擇所屬部門" id="sel_dept" />
<div class="hide" id="sel_dept_sh" style="display: none;">
<p>
<font>深圳市公司 </font>
</p>
<p>
<font>集團管理層 </font>
</p>
</div>
</div>
<script type="text/javascript">
$(".sel_box").click(function(event){
if(event.target.id == 'sel_dept'){
$("#sel_dept_sh").show(); //顯示下拉框
$("#sel_dept_sh p font").click(function(){
$("#sel_dept").val('');
var text = $(this).text();
$("#sel_dept").val(text).css("color","#000");
$("#sel_dept_sh").hide();
});
}else{
$("#sel_dept_sh").hide();
}
});
$(".sel_box").bind("mouseleave",function(){//用mouseleave就實現了模仿下拉框的效果
$(this).find(".hide").hide();
});
$(".sel_box").bind("mouseout",function(){//而mouseout則不行,什麼時候都會觸發
$(this).find(".hide").hide();
});
</script>

正是因為 mouseleave 只在使用者真正離開整個元件時才觸發,而 mouseout 在滑鼠移入子元素時也會觸發,所以製作下拉選單時應使用 mouseleave,才能避免滑鼠移到子項目時選單被誤關。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved