客戶端 X 滑鼠事件在 JavaScript 中充當什麼角色?


當觸發滑鼠事件時,使用客戶端 X 滑鼠事件屬性獲取滑鼠指標的水平座標。這是根據當前視窗進行的。

示例

你可以嘗試執行下面的程式碼瞭解如何在 JavaScript 中實現clientX  滑鼠事件。

<!DOCTYPE html>
<html>
   <body>
      <p onclick = "coordsFunc(event)">
         Click here to get the x (horizontal) and y (vertical) coordinates
         (according to current window) of the mouse pointer.
      </p>
      <script>
         function coordsFunc(event) {
            var x_coord = event.clientX;
            var y_coord = event.clientY;
            var xycoords = "X coords= " + x_coord + ", Y coords= " + y_coord;
            document.write(xycoords);
         }
      </script>
   </body>
</html>

更新於: 2020 年 5 月 23 日

閱讀 303 次

開啟你的 事業

完成課程後獲得證書

開始吧
廣告