我有像
这样的javascriptfunction 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的错误吗?或者任何人都可以建议我另一个跨浏览器光标位置脚本?
答案 0 :(得分:3)
您应该将事件传递给getCursorPosition方法:
document.onmouseup = function(e){
cursor = getCursorPosition(e); //<== added the "e" argument here
alert(cursor.x + ':' + cursor.y);
};
答案 1 :(得分:1)
或完全丢失getCursorPosition()
并使用极其跨浏览器的jQuery:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function jQueryMain ()
{
$(document).mouseup (function (evt) {alert (evt.pageX + ':' + evt.pageY);} );
}
$(document).ready (jQueryMain);
</script>