JS点击其他区域隐藏——应用级

http://www.uw3c.com/jsviews/js57.html

<div id="sbt">search</div>
<div id="search" style="display: block;">点击其他区域隐藏</div>
<script type="text/javascript">
    $(function(){
        $("#sbt").click(function(e){
            $("#search").show();
            var ev = e || window.event;
            if(ev.stopPropagation){
                ev.stopPropagation();
            }
            else if(window.event){
                window.event.cancelBubble = true;//兼容IE
            }
        })
        document.onclick = function(){
            $("#search").hide();
        }
        $("#search").click(function(e){
            var ev = e || window.event;
            if(ev.stopPropagation){
                ev.stopPropagation();
            }
            else if(window.event){
                window.event.cancelBubble = true;//兼容IE
            }
        })
    })
</script>

 上面的程序IE11有时间不灵改了一下的写的方式

$(document).bind('click',function(){
                $('#test').css('display','none');
            });

完整如下;

<script type="text/javascript">
            function stopPropagation(e) {
                if (e.stopPropagation) 
                    e.stopPropagation();
                else 
                    e.cancelBubble = true;
            }

            $(document).bind('click',function(){
                $('#test').css('display','none');
            });

            $('#test').bind('click',function(e){
                stopPropagation(e);
            });
        </script>

链接:http://www.cnblogs.com/dolphinX/p/3239530.html

posted on 2017-03-14 16:04  技术V类  阅读(499)  评论(0编辑  收藏  举报

导航