2013-10-25 84 views
0

我有一個通知div,如果用戶點擊.notification-popup將彈出。我試圖這樣做,popop div會隱藏每當用戶點擊該DIV的一面。jquery - 隱藏div點擊正文

目前我有這樣的:

$('body').on('click', function (e) { 
        $('.notification-list-wrapper').each(function() { 
         //the 'is' for buttons that trigger popups 
         //the 'has' for icons within a button that triggers a popup 
         if (!$(this).is(e.target) && $(this).has(e.target).length === 0 && $('.notification-list-wrapper').has(e.target).length === 0) { 
          $(this).hide(); 
         } 
        }); 
       }); 

當我點擊.notification-彈出雖然隨着這個沒什麼甚至發生。

我應該改變什麼?

回答

2

一種解決方案是,以防止從通知包裝點擊事件傳播,並隱藏在任何其他的元件點擊

$(document).on('click', function (e) { 
    $wrapper.hide(); 
}); 
var $wrapper = $('.notification-list-wrapper').click(function (e) { 
    e.stopPropagation() 
}) 

另一個可能是測試點擊是否在點擊處理程序發生的包裝內

$(document).on('click', function (e) { 
    if ($(e.target).closest($wrapper).length == 0) { 
     $wrapper.hide() 
    } 
}); 
var $wrapper = $('.notification-list-wrapper'); 
+0

你打我幾秒鐘,darn:P – Terry