jQ中的mouseleave和mouseout的区别 模仿下拉框效果

十度 JQuery 2016年02月17日 收藏

1.不论鼠标指针离开被选元素还是任何子元素,都会触发 mouseout 事件。

2.只有在鼠标指针离开被选元素时,才会触发 mouseleave 事件。

  1. <div class="sel_box">
  2.     <input type="button" value="请选择所属部门" id="sel_dept" />
  3.     <div class="hide" id="sel_dept_sh" style="display: none;">
  4.         <p>
  5.            <font>深圳市公司 </font>
  6.         </p>
  7.         <p>
  8.             <font>集团管理层 </font>
  9.         </p>
  10.     </div>
  11. </div>
  12.  
  13. <script type="text/javascript">
  14. $(".sel_box").click(function(event){
  15.      if(event.target.id == 'sel_dept'){
  16.          $("#sel_dept_sh").show(); //显示下拉框
  17.          $("#sel_dept_sh p font").click(function(){
  18.              $("#sel_dept").val('');
  19.              var text = $(this).text();
  20.             // alert(text);
  21.              $("#sel_dept").val(text).css("color","#000");
  22.              $("#sel_dept_sh").hide();
  23.          });
  24.  
  25.      }else{
  26.          $("#sel_dept_sh").hide();
  27.      }
  28.      
  29.  
  30. });
  31.  
  32. $(".sel_box").bind("mouseleave",function(){//用mouseleave就实现了模仿下拉框的效果
  33.         $(this).find(".hide").hide();    
  34.     });
  35.  
  36. $(".sel_box").bind("mouseout",function(){//而mouseout则不行,什么时候都会触发
  37.         $(this).find(".hide").hide();    
  38.     });
  39. </script>