开发者

mouse cursor position in Javascript?

How do I get cursor X and Y in javascript?

var $curX=(ns6)?e.pageX : event.clientX+ietruebody().scrollLeft;
var $curY=(ns6)?e.pageY : event.clientY+ietruebody().scrollTop;

I had found this two开发者_JS百科, but they appear as "undefined". Any ideas?

Thanks!


I assume you don't want a framework answer. If not, try this:

document.onclick=function(evt) {
    evt = (evt || event);
    alert(evt.clientX + ' ' + evt.clientY);
}

I wasn't sure what event you wanted, so I just used an onclick event.


The accepted tutorial only link answer points to a broken link. For people coming by searching here


Check out the jquery mousemove api.

You can use the above as

$("#SomeTag").mousemove(function(event) {
  var msg = "Handler for .mousemove() called at ";
  msg += event.pageX + ", " + event.pageY;
  $("#SomeDisplayTag").append("<div>" + msg + "</div>");
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜