Show mouse x and y position with javascript

余生长醉 提交于 2019-12-23 08:00:09

问题


First version does not show me x and y and get I get following error: Uncaught TypeError: Cannot read property 'pageX' of undefined The Second version works but is very similar coded, can't find the problem.

FIRST VERSION (NOT WORKING)

    <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> 

SECOND VERSION (WORKING)

      <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>     

回答1:


On a first sight: clientX should be clientY and missing {} in your else:

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

Also you need to send the event to the function,

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

check this out: http://jsfiddle.net/NLsdZ/1/

Also, use of captureEvents is deprecated, you should use addEventListener instead. More info here: http://forums.asp.net/t/1576872.aspx




回答2:


I had also noticed you are not onloading below, or at least stating that you are.

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();

Should be like/or finally worked for me as:

 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();};
 };


来源:https://stackoverflow.com/questions/12202118/show-mouse-x-and-y-position-with-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!