You can compare the target element with this :
$('#lightbox').click(function(e) { if (e.target === this) { $(this).css('display','none'); } });
e.target is the element that launches the click handler, this is the #lightbox element. So basically you say that you only run the click handler when you click #lightbox . This will also prevent the other children of the #lightbox handler from running above #lightbox .
An alternative would be to add another click handler to the #lightbox > img elements and just stop the click event from spreading and call above:
$('#lightbox > img').click(function(e) { e.stopPropagation(); });
source share