js 获取当前鼠标位置
我用如下代码得不到鼠标的位置,求解释!!!
<canvas id="myCanvas" onclick="getMousePos()"></canvas>
function getMousePos(){
var p=getMousePosition();
alert("x:" + p.MouseX + ",y:" + p.MouseY); //////Alert没有反应
}
function getMousePosition(event) {
var position = {
MouseX: 0,
MouseY: 0
};
if (event.pageX != undefined) {
position.MouseX = event.pageX;
position.MouseY = event.pageY;
}
else {
var target = EventUtil.getTarget(event);
position.MouseX = event.offsetX + getLeft(target);
position.MouseY = event.offsetY + getTop(target);
}
return position;
}
------解决方案--------------------<canvas id="myCanvas" onclick="getMousePos(event)"></canvas>
再修改一下getMousePos的定义。getMousePosition的调用
------解决方案--------------------
被打败。
<canvas id="myCanvas" onclick="getMousePos(event)"></canvas>
function getMousePos(oEvent){
var p=getMousePosition(oEvent);
alert("x:" + p.MouseX + ",y:" + p.MouseY);
}
------解决方案--------------------getMousePosition需要参数,你调用时没有传递
而且不晓得你哪找的代码,感觉根本不对
获取鼠标位置很简单
event.clientX
event.clientY
function getMousePos(){
alert("X:"+event.clientX+","+"Y:"+event.clientY);
}