JavaScript 中 clientX 鼠标事件的作用是什么?
htmljavascriptprogramming scripts更新于 2026/1/14 8:44:17
当触发鼠标事件时,clientX 鼠标事件属性用于获取鼠标指针的水平坐标。这是根据当前窗口的。
示例
您可以尝试运行以下代码,以了解如何在 JavaScript 中实现 clientX 鼠标事件。
<!DOCTYPE html>
<html>
<body>
<p onclick = "coordsFunc(event)">
单击此处获取鼠标指针的 x(水平)和 y(垂直)坐标
(根据当前窗口)。
</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>
相关文章
有用资源
html 参考教程 - 该教程包含有关 html 的更多信息:https://www.cainiaomax.com/html/

