查找与某个元素关联的JavaScript

问题描述:

对于典型的检查元素方法来说这似乎是不可能的,这对HTML和CSS来说似乎很好,但就是这样。我无法转到特定元素,然后链接到控制它的特定JavaScript。有没有办法做到这一点?

This seems to be impossible with the typical "inspect element" approach, which seems great for HTML and CSS, but that's it. I can't go to a particular element and then link to the particular JavaScript that's controlling it. Is there any way to do this?

正如别人所说的那样,JS控制元素没有精确的概念。有一个JS对元素做了一些事情,并且有JS处理元素上的事件。要处理这些情况:

As someone else said, there is no precise notion of "the JS controlling an element". There is JS which does something to an element, and there is JS which handles an event on a element. To handle these cases:


  1. 在Chrome devtools中,选择元素,右键单击,然后选择休息...... 。当元素发生某些事情(例如子元素或其属性发生变化)时,这将会中断,并使您处于进行修改的行。

  1. In Chrome devtools, select the element, right-click, and select Break on.... This will break when something happens to the element, such as a change in its children or its attributes, and leave you on the line that was making the modification.

使用事件侦听器断点并选择中断特定事件。然后在正在侦听它的元素上启动该事件,例如通过单击元素。调试器将带您到处理该事件的行(这可能在jQuery中很深,但这是另一个故事)。

Use "Event Listener Breakpoints" and choose to break on a particular event. Then initiate that event on an element which is listening for it, such as by clicking on the element. The debugger will take you to the line handling that event (which might be deep within jQuery, but that's another story).