Home > Web Front-end > JS Tutorial > body text

jQuery click anywhere hide DIV functionality

小云云
Release: 2018-01-19 13:22:14
Original
1890 people have browsed it

This article mainly introduces the relevant information about jQuery clicking anywhere except the specified area to hide p. The code is simple and easy to understand, very good, and has reference value. Friends who need it can refer to it. I hope it can help everyone.

The specific code is as follows:


$('body').click(function(e) {
  var target = $(e.target);
  // 如果#overlay或者#btn下面还有子元素,可使用
  // !target.is('#btn *') && !target.is('#overlay *')
  if(!target.is('#btn') && !target.is('#overlay')) {
    if ( $('#overlay').is(':visible') ) { 
      $('#overlay').hide();                          
    }
  }
});
Copy after login

or


$('body').click(function(e) {
  if(e.target.id != 'btn' && e.target.id != 'overlay')
   if ( $('#overlay').is(':visible') ) {
     $('#overlay').hide();
   }
})
Copy after login

PS: Next, let’s look at a piece of code jquery. Click to hide other places except itself


$("#test").click(function(e) { 
  e?e.stopPropagation():event.cancelBubble = true; 
}); 
$(document).click(function() { 
  $("#test").fadeOut(); 
<pre name="code" class="html">e?e.stopPropagation():event.cancelBubble = true;  为阻止冒泡事件
});
 

Copy after login

Related recommendations:

jQuery except the specified area Click anywhere to hide the DIV function

js hides the content between divs by class name

Example tutorial of showing hidden divs with css3

The above is the detailed content of jQuery click anywhere hide DIV functionality. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!