修复本机 JavaScript 事件

发布于 2024-12-18 11:12:21 字数 1245 浏览 0 评论 0 原文

我想知道是否有任何成熟的方法来修复浏览器与 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;
    }
};

I'm wondering if there is any well established way to fix browser inconsistencies with DOM events. What is the best way to fix events?

Below is what I am playing with right now (though still broken).

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;
    }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

倾其所爱 2024-12-25 11:12:21

那将是 jQuery

自 2006 年以来浏览器不一致的杀手。

编辑

如果您担心 32kb,请使用 Google 的 CDN。许多网站都使用它,因此该文件很可能会缓存在大多数用户的计算机上。

That would be jQuery.

Slayer of browser inconsistencies since 2006.

Edit

If you are concerned about the 32kb, then use Google's CDN. Lots of sites use it, so there is a good chance that the file will be cached on a majority of your users' computers.

我爱人 2024-12-25 11:12:21

看起来您已经从 jQuery 中提取了代码。 (来自 event.js 的精确行匹配)这似乎是您可以采取的最佳方法。

查看 mouseHookskeyHooks 针对这些事件类型的跨浏览器修复。

It looks like you're already pulling code out of jQuery. (Exact line matches from event.js) This seems like the best approach you could take.

Look at mouseHooks and keyHooks for their cross browser fixes for those event types.

风铃鹿 2024-12-25 11:12:21

看来您正在谈论事件对象,而不是一般事件。

在 javascript 中,原生对象是由 ECMAScript 实现提供的或由 ECMAScript 代码创建的对象(例如 function fred(){}var obj = {})。其他一切都由宿主环境提供,因此是宿主对象(例如窗口、DOM 元素)。

因此,事件对象是一个宿主对象,而不是“原生 javascript”对象。

在大多数编程环境中,合理的方法是只担心直接影响正在开发的程序的事情。该策略只会“修复”事件对象,因为事件对象的不一致会影响代码的特定部分。担心未使用的属性似乎并不明智,这些属性的不一致不会影响正在编写的程序,或者不适用于特定事件(例如,加载事件的 pageX 的值是多少,以及为什么是否应该将其设置为 0 而不是未定义)。

将所有可能的不一致写入单个函数的编程会导致货物邪教编程,其中做特定事情的原因(如果有一个充分的理由来做这些事情的话)会丢失,并且它们会永久存在,只是因为它们存在于“跨浏览器”代码的某些“最佳实践”版本中。

例如,是否有人记得哪个版本的 Safari 将 target 属性设置为文本节点(如果它们是事件目标)?如果不是,并且不存在这样的浏览器正在使用,为什么它仍然在这样的代码中?

如果包含它是因为某些未来的浏览器也可能将文本节点作为事件目标,并且出于某种原因决定事件目标必须始终是元素(或者至少是实现元素接口的节点),那么不是更明智的做法:

if (this.target.nodeType != 1 && this.target.parentNode) {
  this.target = this.target.parentNode;
}

作为对事件目标不是元素的可能性的更一般的修复?

在什么情况下会 event.srcElement == false,以及为什么应将其设置为文档节点而不是 bodyhead 元素?

It seems that you are talking about the event object, not events in general.

In javascript, a native object is one provided by the ECMAScript implementation or created by ECMAScript code (e.g. function fred(){}, var obj = {}). Everything else is provided by the host environment and is therefore a host object (e.g. window, DOM elements).

Therefore the event object is a host object, not a "native javascript" object.

A reasonable approach in most programming environments is to only worry about the things that directly affect the program being developed. That strategy leads to only "fixing" event objects where their inconsistencies affect specific parts of code. It doesn't seem sensible to worry about properties that are not used, whose inconsistencies don't affect the program being written, or that are not applicable to the particular event (e.g. what is the value of pageX for a load event, and why should it be set to 0 rather then left as undefined).

The kind of programming where every possible inconsistency is written into a single function leads to cargo cult programming, where the reason for doing particular things (if ever there was a good reason for doing them at all) is lost and they are perpetuated simply because they exist in some "best practice" version of "cross browser" code.

For example, does anyone remember which versions of Safari set the target property to text nodes if they were the event target? If not, and no such browser exists in use, why is it still in such code?

If it is included because some future browser may also make text nodes event targets, and for some reason it is decided that event targets must always be elements (or at least, nodes that implement the element interface), then wouldn't it be more sensible to do:

if (this.target.nodeType != 1 && this.target.parentNode) {
  this.target = this.target.parentNode;
}

as a more general fix for the possibility that an event target isn't an element?

Under what circumstances would event.srcElement == false, and why should it be set to the document node rather than say the body or head element?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文