我想知道是否有任何成熟的方法来修复浏览器与 DOM 事件的不一致问题。 修复事件的最佳方法是什么?
下面是我现在正在玩的东西(尽管仍然损坏)。
Event.prototype.fix = function()
{
//if ( ! event) var event = window.event;
// Fix target property, if necessary (IE 6/7/8 & Safari2)
if ( ! this.target)
{
this.target = this.srcElement || document;
}
// Target should not be a text node (Safari)
if (this.target.nodeType === 3)
{
this.target = event.target.parentNode;
}
// Add which for key events
if (this.which == null)
{
this.which = this.charCode != null ? this.charCode : this.keyCode;
}
// For mouse/key events; add metaKey if it's not there (#3368, IE6/7/8)
if (this.metaKey === undefined)
{
this.metaKey = this.ctrlKey;
}
this.posx = 0;
this.posy = 0;
if (this.pageX || this.pageY)
{
this.posx = this.pageX;
this.posy = this.pageY;
}
else if (this.clientX || this.clientY)
{
this.posx = this.clientX + document.body.scrollLeft
+ document.documentElement.scrollLeft;
this.posy = this.clientY + document.body.scrollTop
+ document.documentElement.scrollTop;
}
};
最佳答案
那就是jQuery .
自 2006 年以来浏览器不一致问题的终结者。
编辑
如果您担心 32kb,请使用 Google's CDN 。许多网站都使用它,因此该文件很有可能会缓存在大多数用户的计算机上。
关于javascript - 修复 native JavaScript 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8319261/