9

第一个版本没有显示 x 和 y 并得到以下错误:

未捕获的类型错误:无法读取未定义的属性“pageX”

第二个版本有效,但编码非常相似,找不到问题。

第一个版本(不工作)

    <form name ="show">
        <input type="text" name="mouseXField" value="0" size="6">Mouse X Position<br>
        <input type="text" name="mouseYField" value="0" size="6">Mouse Y Position<br>
    </form>
        
        <script type="text/javascript">
        
        var mie = (navigator.appName == "Microsoft Internet Explorer") ? true : false;
        
        if (!mie) {                
            document.captureEvents(Event.MOUSEMOVE);
            document.captureEvents(Event.MOUSEDOWN);
        }
                    
        document.onmousemove = mousePos();
        document.onmousedown = mouseClicked();
        
        var mouseClick;
        var keyClicked;
        
        var mouseX = 0;
        var mouseY = 0;
        
        function mousePos (e) {                
            if (!mie) {
                mouseX = e.pageX; 
                mouseY = e.pageY;
            }
            else
                mouseX = event.clientX + document.body.scrollLeft;
                mouseY = event.clientX + document.body.scrollTop;
                   
        document.show.mouseXField.value = mouseX;
        document.show.mouseYField.value = mouseY;
        
        return true;
        }
        
        </script> 

第二版(工作)

      <form name="Show">
            <input type="text" name="MouseX" value="0" size="4"> X<br>
            <input type="text" name="MouseY" value="0" size="4"> Y<br>
      </form>

        <script type="text/javascript">

            var IE = document.all?true:false

            if (!IE) document.captureEvents(Event.MOUSEMOVE)

            document.onmousemove = getMouseXY;

            var tempX = 0
            var tempY = 0

            function getMouseXY(e) {
                if (IE) {       
                tempX = event.clientX + document.body.scrollLeft
                tempY = event.clientY + document.body.scrollTop
                } 
                else 
                {  
                 tempX = e.pageX
                 tempY = e.pageY
                }  

                document.Show.MouseX.value = tempX
                document.Show.MouseY.value = tempY

                return true
                }
        </script>     
4

2 回答 2

18

乍一看:clientX应该在你clientY的:{}else

else {
   mouseX = event.clientX + document.body.scrollLeft;
   mouseY = event.clientY + document.body.scrollTop;
}

您还需要将事件发送到函数,

document.onmousemove = function (e) {mousePos(e);};
...
function mousePos (e) {
...

看看这个:http: //jsfiddle.net/NLsdZ/1/

此外,captureEvents不推荐使用 of,您应该addEventListener改用。更多信息在这里:http ://forums.asp.net/t/1576872.aspx

于 2012-08-30T17:22:17.217 回答
0

我还注意到你没有在下面加载,或者至少说明你是。

var mie = (navigator.appName == "Microsoft Internet Explorer") ? true : false;

    if (!mie) {

        document.captureEvents(Event.MOUSEMOVE);
        document.captureEvents(Event.MOUSEDOWN);
    }


    document.onmousemove = mousePos();
    document.onmousedown = mouseClicked();

应该像/或最终为我工作:

 window.onload = function () {
         mie = (navigator.appName == "Microsoft Internet Explorer") ? true : false;


    if (!mie) {
         document.captureEvents(Event.MOUSEMOVE);
         document.captureEvents(Event.MOUSEDOWN);
    }
         document.onmousemove = function (e) {mousePos(e);};
         document.onmousedown = function (e) {mouseClicked();};
 };
于 2013-07-11T06:01:24.043 回答