Home >Web Front-end >JS Tutorial >How to control JQuery to hide when clicked outside a div but not when clicked inside the div_jquery

How to control JQuery to hide when clicked outside a div but not when clicked inside the div_jquery

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-05-16 16:20:33967browse

The example in this article describes the method of using JQuery to control the div to be hidden when clicked outside, but not hidden when clicked inside the div. Share it with everyone for your reference. The details are as follows:

For example, there is a div with the id of body, which can be hidden when clicked outside the div but not hidden when clicked inside the div. The implementation is as follows using jQuery:

Copy code The code is as follows:
$("#body").click(function(e) {
$(this).show();
         e.stopPropagation();
});
$(document).click(function(event) {

$("#body").hide();
});

If the div is clicked to hide, jQuery’s trigger event can be used

Copy code The code is as follows:
$(document).trigger("click");

I hope this article will be helpful to everyone’s jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn