Получение позиции курсора с использованием JavaScript не работает в Firefox - PullRequest
2 голосов
/ 14 июля 2010

У меня есть 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?или кто-нибудь может предложить мне другой кросс-браузерный скрипт позиционирования курсора?

Ответы [ 2 ]

3 голосов
/ 14 июля 2010

Вы должны передать событие методу getCursorPosition:

document.onmouseup = function(e){
    cursor = getCursorPosition(e); //<== added the "e" argument here
    alert(cursor.x + ':' + cursor.y);
};
1 голос
/ 14 июля 2010

Или потерять 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>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...