张金玲

博客园 首页 联系 订阅 管理

<!DOCTYPE HTML>

<html>
<head>
<meta charset="UTF-8" />
<title>last.html</title>
<style>
.A {
    width: 100px;
    height: 50px;
    background: red;
}
 
.B {
    width: 500px;
    height: 500px;
    background: blue;
}
</style>
<script type="text/javascript" src="jquery-1.8.0.min.js"></script>
<script type="text/javascript">
    $ (document).ready (function ()
    {
        $ (".B").hide ();
        $ (".A").click (function ()
        {
            $ (".B").toggle ();
        });
    }).click (function (e)
    {
        e = e || window.event;
        if (e.target != $ ('.A')[0] && e.target != $ ('.B')[0])
        {
            $ ('.B').hide ();
        }
    });
</script>
</head>
<body>
    <div class="A">按fffffffff</div>
    <div class="B">ffffffffffff</div>
</body>
</html>
 
方法二
 
$(document).on("click",function(e)
{
if($(e.target).parents("#A").length == 0)
{
$("#B").hide();
}
});
  
 
 
posted on 2016-03-25 13:49  顶顶圈圈  阅读(127)  评论(0)    收藏  举报