編輯
原來,這提供了一個不好的用戶體驗反正,所以請考慮使用此代碼:-)
我能夠創建一個自定義的widget之前功能是這樣的:
jQuery.widget("custom.hoverSelectmenu", jQuery.ui.selectmenu, {
_create: function() {
this._super();
var that = this;
this._on(this.button, {
mouseenter: function(event) {
that.open();
},
mouseleave: function(event) {
if (event.toElement != that.menu.get(0)) {
that.close();
}
}
});
this._on(this.menu, {
mouseleave: function(event) {
if (event.toElement != that.button.get(0)) {
that.close();
}
}
});
}
});
jQuery('.selectbox').hoverSelectmenu({
});
更新
在Firefox,鉻和IE 11下面的作品(低級IE未測試)
_leaveWidget: function(event) {
var target = event.toElement || event.relatedTarget || event.target;
if (!(
jQuery.contains(this.button.get(0), target) ||
jQuery(this.button.get(0)).is(target) ||
jQuery.contains(this.menu.get(0), target) ||
jQuery(this.menu.get(0)).is(target)
)) {
this.close();
}
},
_create: function() {
this._super();
var that = this;
this._on(this.button, {
mouseenter: function (event) {
that.open();
},
mouseout: that._leaveWidget
});
this._on(this.menu, {
mouseout: that._leaveWidget
});
},
使用'。對( '的mouseenter')'代替'.hover()'。 'hover()'添加鼠標輸出功能 –