有个DIV层,通过单击文本框显示出来了,现在我想通过单击窗体的其他地方来把div隐藏掉,该怎么办、

解决方案 »

  1.   

    document.onclick = function(){div.style.display = none};
    或者
    document.onmousemove = function(){div.style.display = none};
      

  2.   

    document.onclick=function(e){
    e=window.event||e;
    obj = e.srcElement ? e.srcElement : e.target;
        if(obj.id!="DIV的id"){
         hideDIV();//这里是你的隐藏操作。
        }
    };
      

  3.   


    感谢您的帮助,我试了您的方法,我的div的z-index:999,我发现点div的下面和右面就一点反映都没有。
    而点它的上面就有用了。这是怎么回事啊?
      

  4.   


    感谢您的帮助,我试了您的方法,我的div的z-index:999,我发现点div的下面和右面就一点反映都没有。 
    而点它的上面就有用了。这是怎么回事啊?
      

  5.   

    应该是因为你点的地方实际上还属于这个DIV。
    给你的文本框定义个ID,把代码里的“DIV的id”换成你的文本框的ID
      

  6.   

    ie6 firefox 下测试通过<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title></title>
        <script type="text/javascript">        function clickHandler(oEvent)
            {
                var layer = document.getElementById("layer");
                layer.innerHTML = new Date().toLocaleDateString();
                layer.style.left = (oEvent.clientX + 10) + "px";
                layer.style.top = (oEvent.clientY + 10) + "px";
                layer.style.display = "block";
                if (oEvent.stopPropagation)
                { oEvent.stopPropagation(); }
                else
                { window.event.cancelBubble = true; }        }        function htmlClickHandler()
            {
                var layer = document.getElementById("layer");
                layer.style.display = "none";
            }
            
            window.onload = function()
            {
                document.documentElement.onclick = function() { document.getElementById("layer").style.display = "none"; }
            }        function layerClickHandler(ctl)
            {
                document.getElementById("inputDomain").value = ctl.innerHTML;
            }
          
        </script>
    </head>
    <body><input id="inputDomain" type="text" onclick="clickHandler(event)" />
    <div id="layer" onclick="layerClickHandler(this)" style="cursor:pointer;position:absolute;border:1px solid #ccc;padding:4px;display:none"></div></body>
    </html>