javascript get x and y coordinates on mouse click

前端 未结 3 2176
甜味超标
甜味超标 2020-11-27 16:07

I have a little div tag that when I click it (onClick event), it will run the printMousePos() function. This is the HTML tags:

<
3条回答
  •  佛祖请我去吃肉
    2020-11-27 16:34

    Like this.

    function printMousePos(event) {
      document.body.textContent =
        "clientX: " + event.clientX +
        " - clientY: " + event.clientY;
    }
    
    document.addEventListener("click", printMousePos);

    MouseEvent - MDN

    MouseEvent.clientX Read only
    The X coordinate of the mouse pointer in local (DOM content) coordinates.

    MouseEvent.clientY Read only
    The Y coordinate of the mouse pointer in local (DOM content) coordinates.

提交回复
热议问题