gpt4 book ai didi

javascript - 使用javascript获取光标位置在firefox中不起作用

转载 作者:行者123 更新时间:2023-11-29 18:35:51 26 4
gpt4 key购买 nike

我有一个类似的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 进行测试时,它不起作用。

在 google 上,许多网站都给了我相同的代码。但它在 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com