javascript - javscript/jquery 点击页面其他地方关闭某个div?
大家讲道理
大家讲道理 2017-04-11 12:44:18
[JavaScript讨论组]

效果类似这样 :

点击一个按钮,弹出一个p,在点击页面其他的位置时,关闭这个p。。。

目前是通过给document一个点击事件,然后关闭这个p。。(ps: 按钮已经阻止事件冒泡。所以不会触发document的事件)

但是我有点疑惑这种做法是否正确?

类似这样的效果还有其他的方式实现吗 ?

代码

javascript: $("#a").click(function(event){ $("#b").show(); event.stop..... (阻止事件冒泡) }); $(document).click(function(){ $("#b").hide() });
大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(10)
黄舟

大概是这个意思吧:

<style>
#a {
    width: 100px;
    height: 100px;
    background-color: blue;
}

#b {
    width: 100px;
    height: 100px;
    background-color: yellow;
}

.hide {
    display:none;
}
</style>
<p id=a></p>
<p id=b class='hide'></p>
<script src="http://cdn.bootcss.com/jquery/1.11.3/jquery.min.js"></script>
<script>
    $(document).click(tap);
function tap(ev) {
    if (ev.target == $('#a')[0]) {
        $('#b').removeClass('hide');
    } else {
        $('#b').addClass('hide');
    }
}
</script>

大概思路就是直接挂document的click事件,抓到以后对比下看是不是那个要点击的p就好了。


“正不正确”其实不是很重要;把功能做出来,能用没bug,那就是正确的。

PHPz

类似于弹窗的那种效果 也就是 点击按钮 显示这个p 点击其他位置 隐藏这个p 没有问题
你写的没错

$("#a").click(function(event){
    $("#b").show();
    event.stop..... (阻止事件冒泡)
});
$(document).click(function(){
$("#b").hide()
});
迷茫
$(document).click(function (e) {
    var target = e.target;
    if (target && $(target).attr("id") === "a") {
        $("#b").show();
    } else {
        $("#b").hide();
    }
});
黄舟
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="jquery.js"></script>
    <style>
        body{
            width: 100%;
            height: 500px;
            padding: 0px;
            margin: 0px;
        }
        .outer{
            position: absolute;
            left:20px;
            top:50px;
            width: 200px;
            height: 200px;
            display: none;
            background-color: #cccccc;
            border: 1px solid darkgrey;
            z-index: 2;
        }

        .button{
            position: absolute;
            left:50px;
            top:100px;
            width: 100px;
            height: 50px;
            background-color: #0a6ebd;
            color:#ffffff;
            font-size: 14px;
            border-radius: 10px;
            display: flex;
            justify-content: center;
            align-items: center;
            cursor: pointer;
        }
    </style>
    <script>
        $(function(){

            $(document).on("click",function(){
                $('.outer').hide();
            });

            $(document).on("click",".button",function(event){
                event.stopPropagation();
                $('.outer').show();
            });

            $(document).on("click",".outer",function(event){
                event.stopPropagation();
            });
        });
    </script>
</head>
<body>

    <p class="outer"></p>
    <p class="button"><span>Show</span></p>
</body>
</html>
ringa_lee

你要的是这样的吗:
http://jsfiddle.net/Nczpb/

ringa_lee

$(’body‘).click(function(){
$("#b").hide()
});

阿神

你在document上添加了一个点击监听事件,那用户点击a标签也会触发这个事件,导致结果就是这个p永远无法显示,你应该监听body点击事件来隐藏

阿神

你的实现方法是正常的思路和解决方法。

不过一般的需求不是还会要求点击该p不会隐藏该p吗?这样的话,那么你还就要再在p上写点击阻止冒泡的js哦。

ringa_lee

一般弹出p最好给后面的页面加遮罩层 给遮罩层绑事件就行了

迷茫
 $(document).on("click", function(ev) {
    var h = $(ev.target);
    if (h.closest("#b").length == 0 && h.closest("#a").length != 1) {
        $("#b").hide()
    }
});
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号