任点关闭div框
<script type="text/javascript"> $(function () { $(document).click(function (e) { if (e.target.id != "show") { $("#show").hide(); } }); $("a").click(function (e) { $("#show").css({left: e.clientX, top: e.clientY}).show(); return false;//防止冒泡 }); }); </script>
1 <script type="text/javascript"> 2 $(document).bind('click',function(e){ 3 var e = e || window.event; //浏览器兼容性 4 var elem = e.target || e.srcElement; 5 while (elem) { //循环判断至跟节点,防止点击的是div子元素 6 if (elem.id && elem.id=='test') { 7 return; 8 } 9 elem = elem.parentNode; 10 } 11 12 $('#test').css('display','none'); //点击的不是div或其子元素 13 }); 14 </script>
1 <script type="text/javascript"> 2 $(document).click(function(e){ 3 var _con = $('.tardiv'); // 设置目标区域 4 if(!_con.is(e.target) && _con.has(e.target).length === 0){ 5 $('.tardiv').remove(); 6 } 7 }); 8 </script>