Home  >  Article  >  Web Front-end  >  jquery implements the method of hiding the drop-down div and mask layer when clicking on other areas_jquery

jquery implements the method of hiding the drop-down div and mask layer when clicking on other areas_jquery

WBOY
WBOYOriginal
2016-05-16 15:24:051336browse

The example in this article describes how jquery implements hiding the drop-down div and mask layer when clicking on other areas. Share it with everyone for your reference, the details are as follows:

For a better user experience, when doing a drop-down to obtain other pop-up layers, when expanding the drop-down, click on other areas to automatically hide the collapse drop-down and mask layers. This effect uses a piece of js That's it.

The following picture is an example of a drop-down menu for reference:

Effect implementation source code:

$(document).bind('click', function(e) {
  var e = e || window.event; //浏览器兼容性
  var elem = e.target || e.srcElement;
  while (elem) {
    //循环判断至跟节点,防止点击的是div子元素
    if (elem.id && elem.id == 'menu') {
      return;
    }
    elem = elem.parentNode;
  }
  //点击的不是div或其子元素
  $('.menuList,.overlay').hide();
});

I hope this article will be helpful to everyone in 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