當我點擊我的菜單之外的文檔時,我的文檔點擊功能沒有隱藏我的菜單。當我點擊img
時,它會顯示菜單,當我再次點擊img
時,它會隱藏它,但當我點擊文檔時,我希望它隱藏菜單,是否有人知道我做錯了什麼以及如何使它工作。文檔點擊隱藏菜單
var visible = false;
var id = $(this).attr('id');
$(document).not('#' + id + ' div:eq(1)').click(function() {
if (visible) {
$('.dropdownlist .menu').hide();
visible = false;
}
});
$(this).find('div:eq(1)').click(function (e) {
var menu = $(this).parent().find('.menu');
if (!visible) {
menu.show();
visible = true;
} else if (visible) {
menu.hide();
visible = false;
}
menu.css({ 'left': $(this).position().left + $(this).width() - menu.find('ul').width(),
'top': $(this).position().top + $(this).height() });
})
,與其合作三江源 – ONYX
表示上下文菜單是否可見一個布爾值,請使用上下文菜單對象的引用。如上所述使用它,但它也告訴你*哪個菜單需要隱藏,當有幾個上下文菜單時這是超級方便的。這不會影響菜單項上的點擊事件。 –