点击除元素以外的任意地方隐藏元素js

比如想实现点击列表弹出筛选器,点击其他任意地方关闭筛选器,如图

 

该筛选器class名

 1  $(document).click(function () {
 2       $(".subMenu").hide();
 3 });
 4  $(".subMenu").on("click", function (event) {
 5       //取消事件冒泡
 6       var e = arguments.callee.caller.arguments[0] || event; //若省略此句,下面的e改为event,IE运行可以,但是其他浏览器就不兼容
 7       if (e && e.stopPropagation) {
 8           // this code is for Mozilla and Opera
 9           e.stopPropagation();
10       } else if (window.event) {
11           // this code is for IE
12           window.event.cancelBubble = true;
13       }
14 });

首先点击document任意位置隐藏该元素,然后给该元素绑定click事件,阻止冒泡到该元素,则可以顺利实现需求。

 

posted @ 2017-05-17 18:29  蓓蕾心晴  阅读(11777)  评论(3编辑  收藏  举报