开发者

In mozilla, event is undefined, IE everything is fine

var mouse = function(e){
    var x = e.pageX || (e.clientX + document.body.scrollLeft);
    var y = e.pageY || (e.clientY + document.body.scrollTop);

    alert(x + " " + y);
};
window.onload = function(e) {
    if(!e) { var e = window.event; }
    mouse(e);
};

This works in IE and I get the proper mouse x and y coordinates. However, when ran in somethi开发者_如何学JAVAng other then IE I just get an alert "NaN NaN"

(I've tried searching)


To me this looks wrong in many ways. I think you want onmousemove and not onload - possibly document.onmousedown - and you do not want to alert. Instead change some innerHTML or document.title

Do you want the click or the move?

document.onmousemove = function(e){
    var x = e.pageX || (e.clientX + document.body.scrollLeft);
    var y = e.pageY || (e.clientY + document.body.scrollTop);
    document.title=x + " " + y;
};

Also have a look here

Responding to the onmousemove event outside of the browser window in IE


This is because load events in most browsers don't have clientX and clientY properties.

Change your onload function to the following:

window.onload = function (e) {
    if (!e) { 
        var e = window.event; 
    }
    alert("type is " + e.type + " clientX is " + e.clientX);
    mouse(e);
};

and you'll see, for non-IE browsers, that the type is a load event, but clientX is undefined.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜