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>");
});
精彩评论