📜  如何在元素上获得相对位置鼠标点击 - 无论代码示例

📅  最后修改于: 2022-03-11 14:56:21.243000             🧑  作者: Mango

代码示例1
document.getElementById('clickme').onclick = function clickEvent(e) {
      // e = Mouse click event.
      var rect = e.target.getBoundingClientRect();
      var x = e.clientX - rect.left; //x position within the element.
      var y = e.clientY - rect.top;  //y position within the element.
      console.log("Left? : " + x + " ; Top? : " + y + ".");
    }