This is slightly better, as it also check the parent(s) of the element being clicked:
$(document).click(function(e) {
var target = e.target;
if (!$(target).is('#menu') && !$(target).parents().is('#menu')) {
$('#menu').hide();
}
});