开发者

JavaScript "normalize event object"

This program taken from a book Pro JavaScript Techniques is used to create hover-like functionality for an Element.

I don`t understand what the author means when he says, in the comments, Normalize the Event object.

Can you tell me

a) why is this necessary, explaining w开发者_开发百科hat would happen if it wasn`t normalized

b) how does the code provide achieve the effect

Thank you.

var div = document.getElementsByTagName('div')[0];

div.onmouseover = div.onmouseout = function(e) {

//Normalize the Event object
e = e || window.event;

//Toggle the background colover of the <div>
this.style.background = (e.type == 'mouseover') ? '#EEE' : '#FFF';

};


It's referring to window.event, IE's non-standard version of the event object. If it weren't normalized, it would break in at least one browser.

What the code does is set e to itself (essentially a no-op), if the parameter is truthy (the event parameter is properly set). If not (in IE), it sets it to window.event.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜