现在页面上有一个显示的div  ,我想 触发  mousedown事件,如果鼠标不是在div的范围内,
那么就把div给隐藏,不知道如何实现,请高手指点!
最好兼容 firefox和ie

解决方案 »

  1.   

    可以给div添加一个attribute,isin,如果鼠标在div范围内就是true,否则是false,当发生mousedown时检测isin就可以了
      

  2.   

    获取某个元素的坐标。
    function findPosition(oElement) {
        var x2 = 0;
        var y2 = 0;
        var width = oElement.offsetWidth;
        var height = oElement.offsetHeight;
       // alert(width + "=" + height);
        if (typeof (oElement.offsetParent) != 'undefined') {
            for (var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent) {
                posX += oElement.offsetLeft;
                posY += oElement.offsetTop;
            }
            x2 = posX + width;
            y2 = posY + height;
            return [posX, posY, x2, y2];    } else {
            x2 = oElement.x + width;
            y2 = oElement.y + height;
            return [oElement.x, oElement.y, x2, y2];
        }
    }
      

  3.   

    [oElement.x, oElement.y, x2, y2]  里面的四个对象  具体是指什么??
    呵呵  我是新手!
      

  4.   

    //获得某个节点的相对坐标
    function GetElementPoint(object)
    {
        var x=0,y=0;
        while(object.offsetParent)
        {
            
            x+=object.offsetLeft;
            y+=object.offsetTop;
            object=object.offsetParent;
        }
        x+=object.offsetLeft;
        y+=object.offsetTop;
        return {'x':x,'y':y};
    }
      

  5.   

    你的思路有点问题,跟获取鼠标无关<body>
    <div id="odiv" style="width:100px;height:100px;border:1px solid red;background-color:#D2D2D2"></div>
    <script type="text/javascript">
    var O = document.getElementById('odiv');
    document.onmousedown = function() {
    O.style.display = "none";
    }
    O.onmousedown = function(e) {
    !!e ? e.stopPropagation() : event.cancelBubble = true
    }
    </script>
    </body>
      

  6.   

    <html>
    <head>
    <title> </title>
    </head>
    <body onmousedown="document.getElementById('div').style.display='none'">
    <div id="div" style="cursor:hand;width:200px;height:200px; background-color:red;" onmousedown="if(window.event) window.event.cancelBubble=true; else arguments[0].stopPropagation();"></div> 
    </body>
    <html> 
      

  7.   

      $(document).mousedown(function(event) {
            var left = calculateOffset(globalSearchSelector, "offsetLeft");
            var top = calculateOffset(globalSearchSelector, "offsetTop");
            var width = globalSearchSelector.width();
            var height = globalSearchSelector.height();
            var top1 = calculateOffset(nav_SearchKeyWord, "offsetTop");
            var height1 = nav_SearchKeyWord.height();
            //在搜索框和提示层范围外清除提示
            if (event.clientX < left || event.clientX > left + width) {
                clearItems();
            }
            else {
                if (event.clientY < top || event.clientY > height + top) {
                    if (event.clientY < top1 || event.clientY > height1 + top1) {
                        clearItems();
                    }
                }
            }
        });