点击页面其他地方隐藏弹窗

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="js/jquery-2.1.3.min.js"></script>
        <style>
            .tip{
                margin:300px auto;
                width:200px;
                height:100px;
                text-align: center;
                background:green;
                color:#fff;
                display:none;
            }
        </style>
    </head>
    <body>
        <button>点我</button>
            <div class="tip">
                这里是提示信息!提示信息!提示信息!
            </div>
        <script>
            $(function(){
                $(‘button‘).click(function(e){
                    $(‘.tip‘).show();

                    e.stopPropagation();
                    $(document).one(‘click‘,function(){
                        $(‘.tip‘).hide();
                    })
                })

                $(‘.tip‘).click(function(e){
                    e.stopPropagation();
                })
            })
        </script>
    </body>
</html>

one() 方法为被选元素附加一个或多个事件处理程序,并规定当事件发生时运行的函数。

当使用 one() 方法时,每个元素只能运行一次事件处理器函数。

时间: 2024-08-07 14:21:57

点击页面其他地方隐藏弹窗的相关文章

jquery实现的点击页面其他地方隐藏显示的元素

jquery实现的点击页面其他地方隐藏显示的元素:在实际应用中,可能有这样的效果,那就是有这样一个弹出层,点击层本身的时候,这个层不会隐藏,而点击除去层之外的页面其他地方则会将这个层隐藏,下面就通过代码实例介绍一下如何实现此效果.代码如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name="author" content="

jquery实现点击页面其他地方隐藏指定元素

jquery实现点击页面其他地方隐藏指定元素:在很多效果中,都有这样的功能,当点击页面的其他地方时,能够隐藏一个指定的元素,例如在模拟实现的select下拉菜单效果中,当下拉菜单出现的时候,我们往往希望当点击页面其他地方的时候,能够隐藏下拉条,下面就通过一个实例单独介绍一下如何实现此功能.代码实例如下: <!DOCTYPE html> <html> <head> <meta charset=" utf-8"> <meta name=

点击页面其它地方隐藏div所想到的jQuery的delegate

在网页开发的过程中经常遇到的一个需求就是点击一div内部做某些操作,而点击页面其它地方隐藏该div.比如很多导航菜单,当菜单展开的时候,就会要求点击页面其它非菜单地方,隐藏该菜单. 先从最简单的开始,假如页面有一个id为test的div,我们要实现点击页面其它地方隐藏该div: <div id="test" style="margin:100px;background-color:#3e3;width:100px;height:100px;"> <

点击页面其它地方隐藏该div的方法

思路一 第一种思路分两步 第一步:对document的click事件绑定事件处理程序,使其隐藏该div 第二步:对div的click事件绑定事件处理程序,阻止事件冒泡,防止其冒泡到document,而调用document的onclick方法隐藏了该div. <script type="text/javascript"> function stopPropagation(e) { if (e.stopPropagation) e.stopPropagation(); else

点击页面其它地方隐藏该div的两种思路

第一种思路分两步 第一步:对document的click事件绑定事件处理程序,使其隐藏该div 第二步:对div的click事件绑定事件处理程序,阻止事件冒泡,防止其冒泡到document,而调用document的onclick方法隐藏了该div. $(document).bind('click',function(){ $('#test').css('display','none'); }); $('#test').bind('click',function(e){ stopPropagati

点击页面其它地方隐藏某个div的两种思路

思路一             第一种思路分两步     第1步:对document的click事件绑定事件处理程序,使其隐藏该div     第2步:对div的click事件绑定事件处理程序,阻止事件冒泡,防止其冒泡到document,而调用document的onclick方法隐藏了该div $(document).bind('click',function(){    $('#test').hide();}); $('#test').bind('click',function(e){    

JS如何实现点击页面其他地方隐藏菜单?

方法一: $("#a").on("click", function(e){  $("#menu").show();  $(document).one("click", function(){  $("#menu").hide();  });  e.stopPropagation(); }); $("#menu").on("click", function(e){  e

android 实现点击listview 空白地方隐藏菜单

思路:重写ListView的setOnTouchListener事件: 1 ListView.setOnTouchListener(new OnTouchListener(){ 2 3 @Override 4 public boolean onTouch(View arg0, MotionEvent arg1) { 5 // TODO Auto-generated method stub 6 hideMenu();//隐藏菜单 7 return false; 8 } 9 10 }); 延申: 点

点击页面其他地方的时候,让弹出框消失

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Typ