代码之家  ›  专栏  ›  技术社区  ›  sumek

在特定div之外的页面上单击任意位置时发生的事件

  •  10
  • sumek  · 技术社区  · 16 年前

    4 回复  |  直到 16 年前
        1
  •  30
  •   Eran Galperin    16 年前

    将单击事件附加到文档以隐藏div:

    $(document).click(function(e) {
       $('#somediv').hide();
    });
    

    将单击事件附加到div以阻止对其的单击传播到文档:

    $('#somediv').click(function(e) {
       e.stopPropagation();
    });
    
        2
  •  2
  •   VonC    16 年前

    this post ):

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"  "http://www.w3.org/TR/html4/loose.dtd">
    <html>
      <head>
        <title>Untitled Document</title>
        <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        <meta http-equiv="Content-Style-Type" content="text/css">
        <meta http-equiv="Content-Script-Type" content="text/javascript">
        <style type="text/css">
          <!--
          #mydiv{
            background-color: #999999;
            height: 100px;
            width: 100px;
          }
          -->
        </style>
        <script type="text/javascript">
          document.onclick=check;
          function check(e)
          {
            var target = (e && e.target) || (event && event.srcElement);
            var obj = document.getElementById('mydiv');
            if(target!=obj){obj.style.display='none'}
          }
        </script>
      </head>
      <body>
        <div id="mydiv">my div</div>
      </body>
    </html> 
    

    通过IE6和FireFox3.1测试,它确实可以像广告中所宣传的那样工作。

        3
  •  0
  •   Eric Wendelin    16 年前

    听起来你确实想要一个模态对话框。这个jQuery插件 http://code.google.com/p/simplemodal/ 看起来它有潜力。

        4
  •  0
  •   Zoredache    16 年前