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.