1. 程式人生 > >html select控制元件移動到option標籤時就觸發mouseleave()解決方法

html select控制元件移動到option標籤時就觸發mouseleave()解決方法

今天在做select標籤滑鼠移出時就自動隱藏的功能,在谷歌下面除錯沒問題,但是到火狐上除錯時發現滑鼠下移到option標籤時就會觸發mouseleave方法,百度了之後找到問題的方法,就是在mouseleave方法中加入一個事件物件,通過判斷滑鼠移動到的物件是否為null或undefined(移動到option標籤上ie下為null,firefox等為undefined)。

程式碼:

$(xxx).mouseleave(function (e) {
                var o = e.relatedTarget || e.toElement;//獲取select標籤物件,移動到option上谷歌貌似option在mouseleave函式上是與select繫結在一起的不會觸發mouseleave事件,ie下是null,firefox等為undefined 
if (!o) return; //增加移動到的物件判斷,o為null或者undefined時(即移動到option時)return,不執行下面的方法 //執行你的程式碼 });