javascript Event.ClientX 在 Firefox 和 chrome 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10045351/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 08:32:59  来源:igfitidea点击:

Event.ClientX is not working in firefox and chrome

javascript

提问by user1316760

I have coding which is working in IE but not in firefox and chrome...

我的编码在 IE 中有效,但在 Firefox 和 chrome 中无效...

 function handleWindowClose() {
            if ((window.event.clientX < 0) || (window.event.clientY < 0))
             {
                 event.returnValue = "Are You sure to leave this page";
             }
         }
         window.onbeforeunload = handleWindowClose;

Can anyone help me...

谁能帮我...

回答by ccpizza

window.eventis a IE-only thing. To get it to work in other browsers you have to get the event as an argument of the handler function:

window.event是 IE 专用的东西。要使其在其他浏览器中工作,您必须将事件作为处理程序函数的参数获取:

function handleWindowClose(e) {
    e = window.event || e; 
        if ((e.clientX < 0) || (e.clientY < 0))
        {
            e.returnValue = "Are You sure to leave this page";
        }
}
window.onbeforeunload = handleWindowClose;

回答by arpad

maybe just add mousemove handler that will store mouse position in variable

也许只需添加将鼠标位置存储在变量中的 mousemove 处理程序

var mouse;
function storeMouse(e)
{
    if(!e) e = window.event;
    mouse = {clientX: e.clientX, clientX:e.clientY};
}
function test(e){
     alert(mouse.clientX);
}

and use jquery?

并使用jquery?

  $(window).bind('beforeunload', function() {
    if (iWantTo) {
        return 'Are You sure to leave this page';
    }
});