2016-05-11 32 views
2

我有popovers這樣的:當外部(主體)點擊關閉酥料餅

<div class="slbb-popover popover top" role="tooltip"> 
    <div class="arrow"></div> 
    <h2 class="popover-title">Speelland</h2> 
    <div class="popover-content">And here's some amazing content. It's very engaging. Right? </div> 
    <div class="text-center"><a class='btn btn-primary'>Meer informatie</a></div> 
</div> 

我觸發它們在JavaScript和給他們顯示塊。現在我想將其關閉,當我點擊外(體),但這個代碼只會做一次:

$('body').click(function(e){ 
    if(e.target.className == "btn-pointer") 
     return; 

    //For descendants of menu_content being clicked, remove this check if you do not want to put constraint on descendants. 
    if($(e.target).closest('.btn-pointer').length) 
     return; 

    console.log('check1'); 

    //Do processing of click event here for every element except with id menu_content 
    if($('.popover').css('display') == 'block') { 
     $('.popover').css('display', 'none'); 
     console.log('check2'); 
    } 
}); 

回答

1

試試這個示例:

$(function() { 

    var 
    status = false; 

    $('body').click(function() { 

    if (!status) { 

     // VIEW POPUP WINDOW 
     status = true; 
    } 
    else { 

     // CLOSE POPUP WINDOW 
     status = false; 
    } 

    }); 
}); 

Demo (with window.open())