2012-06-26 62 views

回答

5

一個簡單的例子:

HTML

<div id="target"> 
    Your div 
    <span>A span</span> 
    <div> 
     Another child div 
    </div> 
</div> 

的jQuery

function hideDiv(e) { 
    if (!$(e.target).is('#target') && !$(e.target).parents().is('#target')) { 
     $('#target').hide(); 
    } 
} 
$(document).on('click', function(e) { 
    hideDiv(e); 
}); 

Working sample