日期:2014-05-16  浏览次数:20291 次

js关闭层问题,求大神指教啊,急~~~
我页面上有一些隐藏层,点击某个按钮就弹出来,就是让它的display值从none变成block。比如表情,现在要求在点击除表情区域以外的网页位置,要使表情div关闭掉,这个js咋个处理啊

------解决方案--------------------
HTML code

<!doctype html>
<html>
    <head>
        <meta charset="gb2312" />
        <title></title>
        <style>
            span {
                color:red;
            }
            #test {
                display:none;
                position:absolute;
                left:30px; top:30px;
                width:200px;
                border:1px solid red;
                background:#fff;
            }
        </style>
    </head>
    <body>
        <span id="span">打开层</span>
        <div>1-1111111111</div>
        <div>1-1111111111</div>
        <div>1-1111111111</div>
        <div>1-1111111111</div>
        <div>1-1111111111</div>
        <div id="test">
            <a href="#">123</a>
            浮层,点击这个浮层以外的区域,都可以隐藏这个浮层
        <div>
        <script>
            function $(o){return document.getElementById(o)}
            $('span').onclick = function(e){
                $('test').style.display = 'block';
                e = e || window.event;
                if (e.stopPropagation) {
                    e.stopPropagation();
                } else {
                    e.cancelBubble = true;
                }

            }
            var odiv = $('test');
            document.onclick = function(e){
                e = e || window.event;
                var s = e.target || e.srcElement;
                if( e.srcElement ){ //ie
                    if( !(s == odiv || odiv.contains(s)) ){
                        odiv.style.display = 'none';
                    }
                }else{
                    var res = odiv.compareDocumentPosition(s); 
                    if( !(s == odiv || res == 20 || res == 0) ){
                        odiv.style.display = 'none';
                    }
                }
                
            }            
            
            
        </script>        
    </body>
</html>