点击div之外的地方div消失

2008年3月19日 – 02:17
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4.     <script type="text/javascript" src="http://shawphy.com/common/jquery.js"></script>
  5. </head>
  6. <body style="background:black;width:100%;height:100%;margin:0;padding:0">
  7.     <div id="showdiv" style="background:white">click here to show a div.</div>
  8.     <div id="test" style="background:white;display:none;width:150px;height:150px;">
  9.         <ul id="">
  10.         <li>list 1</li>
  11.         <li>list 2</li>
  12.         </ul>
  13.     <a href="#">close</a>
  14. </div>
  15. </body>
  16. <script type="text/javascript">
  17. <!--
  18. $(function() {
  19.  
  20.     $("#showdiv").click(function(e) {
  21.         if ($("#test").is(":hidden")) {
  22.             $("#test").fadeIn();
  23.             e.stopPropagation();
  24.         }
  25.     });
  26.     $("#test").click(function(e) {
  27.         e.stopPropagation();
  28.     });
  29.     $(document).click(function() {
  30.         $("#test").fadeOut();
  31.     });
  32.     $("#test a").click(function() {
  33.         $("#test").fadeOut();
  34.     });
  35. })
  36. //-->
  37. </script>
  38. </html>

暂时没有找到纯JQ的方法,只能采用事件处理机制来阻止冒泡。
可以在这里看效果 http://shawphy.com/demo/closediv/closediv.html

===================================
事后发现其实JQ已经为此做了兼容性,具体可以参考http://shawphy.com/40.html

发表评论

您宝贵的评论将会在审核后立马发布,别担心~~马上就出来~~~感谢您的关注~~