检查jQuery元素事件

    技术2022-07-11  115

    Building on top of other tools can be incredibly difficult, especially when you didn't create the other tool and you can't replace that tool.  And when those other tools create loads of event listeners, you sometimes see odd behavior within the page and have no idea what the hell is going on.  Unfortunately a large part of client side coding and library usage comes down to fighting your own tools.

    在其他工具之上进行构建非常困难,尤其是当您没有创建其他工具并且无法替换该工具时。 当其他工具创建事件监听器负载时,有时您会在页面中看到奇怪的行为,并且不知道到底发生了什么。 不幸的是,客户端编码和库使用的很大一部分归结于使用自己的工具。

    Luckily jQuery allows you inspect events that have been registered to a given element!  Here's the magic:

    幸运的是,jQuery使您可以检查已注册到给定元素的事件! 这是魔术:

    // First argument is the element you want to inspect jQuery._data(document.body, "events");

    What's returned is an object whose keys represent the event names and the values are arrays of event handles that have been registered to the element and in the order they were registered.  You can even inspect the function URL location and its contents, then allowing you to see what code is messing with your page.  And then, after you've cursed out the other tool, you can monkey patch the problematic function.

    返回的对象是一个对象,其键代表事件名称,值是事件句柄的数组,这些事件句柄已按元素的注册顺序进行注册。 您甚至可以检查函数URL的位置及其内容,然后让您查看导致页面混乱的代码。 然后,在诅咒了另一个工具之后,您可以猴子修补有问题的功能。

    Event listeners can really cause debugging misdirection within JavaScript, especially when you aren't an expert with a given framework.  Take the time to learn to leverage as many helper methods as you can -- they will save you hours of frustration.

    事件监听器确实会导致JavaScript内的调试错误定向,尤其是当您不是给定框架的专家时。 花时间学习尽可能多地利用辅助方法-它们将节省您数小时的挫败感。

    翻译自: https://davidwalsh.name/inspect-jquery-events

    Processed: 0.009, SQL: 9