求教各位高手啊要实现一个功能,页面中有一个button,click时弹出一个div的dialog,现在要求当div弹出时,鼠标点击页面中除了div的其他区域时,div关闭求教方法啊?取得鼠标点击时坐标,可以和div的长宽来比较么?多谢各位了。

解决方案 »

  1.   

    把这句加上div后面 onmouseout="javascript:if(!this.contains(event.toElement)){document.getElementById('div').style.display:none;}"
      

  2.   


    onmouseout是鼠标一离开就关闭了啊?
    我想要点击别的地方的啊
    我换成了onmousedown结果不好用了
    555555555
      

  3.   

    可以在文档的单击事件中隐藏document.onclick = function(){
        //隐藏代码
    };然后在div的单击事件中取消事件冒泡document.getElementById("divid").onclick = function(oEvent){
        if(document.all){
            event.cancelBubble = true;
        }else{
            oEvent.stopPropagation();
        }
    };
      

  4.   

    研究一下关于窗体的事件处理吧,就是跟event有关的东西,我下班了,如果你明天搞不定,我再看看。
      

  5.   

    蹭点分。
    <html>
    <body>
    <input type="button" value="Zswang 路过!" onclick="button_Click()" />
    <div id="div_panel" style="display:none;width:200px;height:200px;background-color:Red;">
    <a href="http://blog.csdn.net/zswang" alt="广告">http://blog.csdn.net/zswang</a>
    <br />
    <img src="http://www.csdn.net/Images/logo_csdn.gif" alt="CSDN"/>
    </div>
    <script type="text/javascript">
    function $(id) { return document.getElementById(id); }function addEventHandler(target, type, func) {
        if (target.addEventListener)
            target.addEventListener(type, func, false);
        else if (target.attachEvent)
            target.attachEvent("on" + type, func);
        else target["on" + type] = func;
    }function removeEventHandler(target, type, func) {
        if (target.removeEventListener)
            target.removeEventListener(type, func, false);
        else if (target.detachEvent)
            target.detachEvent("on" + type, func);
        else delete target["on" + type];
    }function button_Click() {
    $("div_panel").style.display = "";
    addEventHandler(document, "mousedown", document_MouseDown);
    }function document_MouseDown(e) {
    var element = typeof event != "undefined" ? event.srcElement : e.target;
    var downPanel = false;
    while (element) {
    downPanel = element == $("div_panel");
    if (downPanel) break;
    element = element.parentNode;
    }
    if (!downPanel) {
    removeEventHandler(document, "mousedown", document_MouseDown);
    $("div_panel").style.display = "none";
    }
    }
    </script>
    </body>
    </html>
      

  6.   

    服了,真是高人啊,我改了改用到工程里了
    我想知道
    function $(id) { return document.getElementById(id); }
    有什么用呢?
    而且我用$("div_panel")都不行,都换成document.getElementById('search')才可以啊
    jQuery么?
      

  7.   

    囧 $("div_panel"),document.getElementById('search')
    这两个是等价的,至少id你得改改吧。
      

  8.   

    能否再加上那个隐藏DIV显示时的位置?
    设置为动态位置显示,而不是直接显示在下方。