如何从鼠标捕获功能中获取返回值?

时间:2010-06-02 18:22:50

标签: javascript

如何从鼠标捕获功能中获取返回值?

        function getMousePosition(e) 
        {
            positionX = e.pageX;
            positionY = e.pageY;

            if (positionX < 0){positionX = 0;}
            if (positionY < 0){positionY = 0;} 

            return positionX + " " + positionY;
        }

var mousePositions = getMousePosition(e);

一样

2 个答案:

答案 0 :(得分:1)

 function getMousePosition(e)  
        { 
            e = e || event //for cross browser implentation
            var positionX = e.pageX; 
            var positionY = e.pageY; 

            if (positionX < 0){positionX = 0;} 
            if (positionY < 0){positionY = 0;}  

            return {positionX: positionX, positionY: positionY}; 
        } 

现在你可以这样做

var mousePositions = getMousePositions(someEventObject);

alert(mousePositions.positionX);
alert(mousePositions.positionY);

答案 1 :(得分:0)

您必须将onmousemove事件监听器分配给您的函数,如下例所示:

document.onmousemove = getMousePosition();