弹出层---Jquery判断是否为某元素或其子元素

来源:互联网 发布:斯维尔暖通负荷软件 编辑:程序博客网 时间:2024/04/30 02:18

用于当点击弹出层时弹出层不消失,而点击其他地方时弹出层消失

//判断是否为某元素子元素,是则返回true

jQuery.fn.isChildOf = function(b){ 
 return (this.parents(b).length > 0); 
 };

//判断是否为某元素或其本身,是则返回true

jQuery.fn.isChildAndSelfOf = function(b){ 
 return (this.closest(b).length > 0);//closest(b)从当前元素开始,沿 DOM 树向上遍历,返回符合b的的元素;parent(b)则是从父元素开始,故不包含自身
 };
 
 $('body').bind("click", function(e) {
 if(!$(e.target).isChildAndSelfOf(".tip"))//.tip是弹出层


   {
            e.stopPropagation();//阻止JS冒泡
            $(".tip").remove();              
       }
                   
     });
    $('.lianjie').bind("click",function(e){//.lianjie是出现弹出层的按钮
         e.stopPropagation();
     });

原创粉丝点击