跨浏览器事件对象规范化?

跨浏览器事件对象规范化?

问题描述:

我正在寻找一个好的资源对事件对象的事件规范化。我试图自己做,但我一直觉得我会错过什么。

I'm looking for a good resource on event normalization on the event object. I'm trying to do it myself but I keep feeling like I'm going to miss something.

这是我到目前为止,告诉我,如果我错过任何东西。

Here's what I have so far, tell me if I missed anything.

var eFix = function(e) {
    e = e || window.event;
    e.target = e.target || e.srcElement;
    e.offsetX = e.offsetX || e.layerX;
    e.offsetY = e.offsetY || e.layerY;
    e.relatedTarget = e.relatedTarget ||
        e.type == 'mouseover' ? e.fromElement : e.toElement;
    e.target = e.target || e.srcElement;
    if (target.nodeType === 3) target = target.parentNode; //Safari bug
    return e;
};

有没有人看到完整的正规化函数?我错过了什么吗?
(不用说我们要去的不是IE的W3C模型)

Has anyone seen a complete normalization function? Did I miss anything? (Needless to say we're going for W3C model not IE)

e.layerX 仅适用于定位的元素,因此至少你需要为你的元素添加一个position:relative函数。其次, e.offsetX 只能在 IE8和更高版本中正常工作,所以你应该尽量避免使用它(虽然我现在正在使用它们,但这只需要在特定的浏览器中工作。)

e.layerX only works on positioned elements, so at the very least you need to add a "position:relative" to your element to function. Secondly e.offsetX only works correctly in IE8 and later, so you should probably refrain from using it either way (although I am using them right now, but this only needs to work in specific browsers).