0

我想在光标移动时显示某个站点的坐标 x,y ..

我使用波纹管脚本,但它会生成一个警报框。

<script type=text/javascript>
var isIE = document.all?true:false;
if (!isIE) document.captureEvents(Event.CLICK);

document.onclick = getMousePosition;
function getMousePosition(e) {
  var _x;
  var _y;
  if (!isIE) {
    _x = e.pageX;
    _y = e.pageY;
  }
  if (isIE) {
    _x = event.clientX + document.body.scrollLeft;
    _y = event.clientY + document.body.scrollTop;
  }
  posX = _x;
  posY = _y;

  return true;
}
</script>

<body onclick=alert("X-position:&nbsp;"+posX+";&nbsp;Y-position:&nbsp;"+posY+".")>
4

2 回答 2

1

这应该有效:

// Cursor coordinate functions
var myX, myY, xyOn, myMouseX, myMouseY;
xyOn = true;

function getXYPosition(e) {
    myMouseX = (e || event).clientX;
    myMouseY = (e || event).clientY;
    if (document.documentElement.scrollTop > 0) {
        myMouseY = myMouseY + document.documentElement.scrollTop;
    }
    if (xyOn) {
        alert("X is " + myMouseX + "\nY is " + myMouseY);
    }
}
function toggleXY() {
    xyOn = !xyOn;
    document.getElementById('xyLink').blur();
    return false;
}   

document.onmouseup = getXYPosition;

您还需要这个隐藏的超链接:

<a href="#" id="xyLink" onfocus="toggleXY();" accesskey="z"></a>

在此处输入图像描述

这是文章:http ://www.brenz.net/snippets/xy.asp

于 2011-11-11T19:14:35.523 回答
1

我认为这是一个更简单的选择..

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
   $(document).mousemove(function(e){
      $('#status').html(e.pageX +', '+ e.pageY);
   }); 
})
</script>
<body>
<h2 id="status">
0, 0
</h2>
</body>
</html>

http://docs.jquery.com/Tutorials:Mouse_Position#Tracking_mouse_position

于 2011-11-11T18:35:47.303 回答