我有一个像这样的JavaScript
function getCursorPosition(e) {
e = e || window.event;
var cursor = {x:0, y:0};
if (e.pageX || e.pageY) {
cursor.x = e.pageX;
cursor.y = e.pageY;
}
else {
cursor.x = e.clientX +
(document.documentElement.scrollLeft ||
document.body.scrollLeft) -
document.documentElement.clientLeft;
cursor.y = e.clientY +
(document.documentElement.scrollTop ||
document.body.scrollTop) -
document.documentElement.clientTop;
}
return cursor;
}
document.onmouseup = function(e){
cursor = getCursorPosition();
alert(cursor.x + ':' + cursor.y);
};
此代码提醒单击光标的 X 和 Y 位置。这在 IE 7/8、Chrome/Safari、Opera 10 中效果很好。但在使用 firefox 4.0 beta 1 进行测试时,它不起作用。
在谷歌上搜索,许多网站给了我相同的代码。但它在 ff 4.0b 中不起作用
这是 ff 4.0b 的错误吗?或者有人可以建议我另一个跨浏览器光标位置脚本吗?
最佳答案
您应该将事件传递给 getCursorPosition 方法:
document.onmouseup = function(e){
cursor = getCursorPosition(e); //<== added the "e" argument here
alert(cursor.x + ':' + cursor.y);
};
关于javascript - 使用 javascript 获取光标位置在 Firefox 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3244194/