I'm creating a simple light box to display photos. When you click on an image in a gallery it expands to take up most of the screen while blacking out all content of the page behind. To do this I just append a div
with the image clicked within it to the DOM like this:
<div id='image'>
<img src="someImage">
</div>
I'm trying to create another onclick event that hides this DIV, but only if the click is anywhere other than the image. So if you click on the image I don't want the div to disappear....only if you click on any of the blacked out portion to the sides of the image. I tied the onclick to the "body" like so:
$('body').click(function(){
$('#image').hide();
}):
The problem is even if I click the image it disappears. How can I disable this?
Try stopping the event from propagating to the body from the image.
For example:
$('#image').on('click', function (e) {
e.stopPropagation();
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.