检测鼠标单击位置

问题描述

| 我要实现的是制作DIV并在其上或作为背景显示世界地图图像。&当用户单击地图上的某些位置时,我会捕获鼠标单击位置, 然后我将介绍如何将鼠标的X,Y线转换为经度,纬度,然后运行AJAX来显示一些数据。 我的问题是: 如何使用JavaScript获取鼠标单击位置 如何将全局点击位置转换为DIV的相对X,Y层坐标     

解决方法

        看这个例子: 单击图像并使用Javascript获取坐标 以下帖子可能也有帮助: 使用jQuery获取链接图像点击的准确位置 创建HTML图像映射     ,        在HTML中,您设置onMouseMove事件:
<div onMouseMove=\"getPositions();\"
style=\"width:200px;height:100px\"></div>
和javascript函数:
function getPositions(ev) {
if (ev == null) { ev = window.event }
   _mouseX = ev.clientX;
   _mouseY = ev.clientY;
}
    ,        除非您有从头开始的理由,否则我建议使用Google Maps Api。 您会注意到Map对象具有一个click事件,您可以将其绑定到一个回调,该回调接收一个包含经/纬度坐标的MouseEvent。看看这个例子。     ,        在事件处理程序中,您可以通过mouseEvent Args获取它。 摘录自http://msdn.microsoft.com/zh-cn/library/bb404721.aspx-
function onMouseLeftButtonUp(sender,mouseEventArgs)
{
    // Return a Point object representing the x- and y-coordinates of the current mouse position
    // relative to the reference object.
    var pt = mouseEventArgs.getPosition(sender.findName(\"referenceObject\"));

    // Display the current mouse position.
    sender.findName(\"Status\").text = pt.x + \" : \" + pt.y;
}