简体   繁体   English

当我逐步浏览Chrome中的JavaScript断点时,如何查看DOM?

[英]How can I view the DOM while I am stepping through JavaScript breakpoints in Chrome?

In Chrome DevTools, while debugging JavaScript in the Sources tab(adding the "debugger;" line in JS code and then stepping through the code using F10/F11), how can I view the DOM while stepping through the code? 在Chrome DevTools中,在Sources选项卡中调试JavaScript(在JS代码中添加“debugger;”行,然后使用F10 / F11逐步执行代码),如何在单步执行代码时查看DOM?

If my JS is manipulating the DOM, it is very common that I need to step through the JS debugger and watch how the DOM elements are modified by my JS. 如果我的JS正在操作DOM,那么我需要逐步浏览JS调试器并观察我的JS如何修改DOM元素是很常见的。 For example, I might have to see how elements are being moved, whether they are being removed when they are supposed to, whether they are getting the correct class at the correct time, etc. 例如,我可能必须查看元素是如何移动的,它们是否应该被删除,它们是否在正确的时间获得正确的类等。

Having to switch back and forth between the Sources tab to execute a single line and then Elements tab to see how the DOM was modified for every line I execute gets in the way of my debugging and keeps me from being able to tell how each line is affecting the DOM. 必须在Sources选项卡之间来回切换以执行单行,然后在Elements选项卡中查看如何为我执行的每一行修改DOM,这会影响我的调试并使我无法告诉每一行是怎么回事影响DOM。

How can I view the elements while stepping through code simultaneously? 如何同时单步执行代码时查看元素?

MutationObserver MutationObserver

I don't think DevTools (as of Chrome 59) is equipped to handle your need (at the moment), but a MutationObserver might help. 我不认为DevTools(从Chrome 59开始)可以满足您的需求(目前),但MutationObserver可能会有所帮助。

Execute the following code in the DevTools Console (or save it as a snippet): 在DevTools控制台中执行以下代码(或将其另存为代码段):

var target = document.querySelector('body');
var observer = new MutationObserver(function(mutations) {
  mutations.forEach(function(mutation) {
    console.log(mutation);
  });    
});
var config = { 
  childList: true, 
  attributes: true, 
  characterData: true, 
  subtree: true, 
  attributeOldValue: true, 
  characterDataOldValue: true
};
observer.observe(target, config);

This is about as verbose as it gets. 这就像它得到的一样冗长。 It logs every change to body or any of its descendants. 它将每个更改记录到body或其任何后代。 You can tweak the code to track less changes (eg by observing a more specific node, or turning off the config flags). 您可以调整代码以跟踪更少的更改(例如,通过观察更具体的节点,或关闭配置标志)。

See MutationObserverInit for a description of all of the config flags. 有关所有配置标志的说明,请参见MutationObserverInit There's also an attributeFilter flag (not used in the code sample) which may be of use to you. 还有一个attributeFilter标志(代码示例中未使用)可能对您有用。

DOM Breakpoint DOM断点

Another option is to set a "subtree modification" DOM Breakpoint on a node. 另一种选择是在节点上设置“子树修改” DOM断点 DevTools pauses whenever the node or any of its children is added or removed, or its contents are changed. 每当添加或删除节点或其任何子节点或其内容发生更改时,DevTools都会暂停。 However, it doesn't track attribute modifications, so that may not be enough for this scenario. 但是,它不跟踪属性修改,因此对于此方案可能不够。

I think I might do is instead type debugger in your js, go to Elements view in dev tool, you can right-click on the element you want to debug, and select the sub menu break on, there are several options for you to debug your DOM. 我想我可能会在你的js中输入调试器,转到dev工具中的Elements视图,你可以右键单击要调试的元素,然后选择子菜单中断,有几个选项供你调试你的DOM。

Also, you can check the element you want to debug in console by $("selector") 此外,您可以通过$(“selector”)检查要在控制台中调试的元素

Not sure if I got you right, but you can refer to DOM inspection from the Sources tab, without having to switch tabs. 不确定我是否帮助您,但您可以从“源”选项卡参考DOM检查,而无需切换选项卡。

In the Sources tab, you can show the console (pressing ESC worked for me), and then refer to the latest DOM changes by $0 .. $4, or any other DOM by selector. 在Sources选项卡中,您可以显示控制台(按ESC为我工作),然后通过$ 0 .. $ 4或选择器的任何其他DOM引用最新的DOM更改。 Please refer to Chrome's Command Line API Reference 请参阅Chrome的命令行API参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 chrome 扩展 javascript 操作 DOM - How can I manipulate the DOM using the chrome extension javascript 如何在Chrome中设置DOM断点 - how to set DOM Breakpoints in chrome 如何在Chrome开发人员工具中的每一行上不设置断点的情况下,逐行浏览JavaScript代码? - How do I step through JavaScript code line by line without placing breakpoints on each line in Chrome developer tools? 如何通过javascript启动Chrome打包应用? - How can I launch a Chrome Packaged App through javascript? 在逐步执行Javascript AngularJS代码时如何防止显示黑框脚本 - How do I prevent black boxed scripts from showing when stepping through Javascript AngularJS code 编写Chrome扩展程序时如何调试Javascript错误? - How can I debug Javascript errors while writing Chrome extensions? Google Chrome:如何在按住鼠标键的同时查看DOM的状态 - Google Chrome: How can I watch the state of the DOM while holding the mouse button 如何在Chrome Developer中查看DOM对象的属性? - How do I view the properties of a DOM object in Chrome Developer? 在Chrome Devtools中调试Javascript时,遇到DOM断点后如何继续? - When debugging Javascript in Chrome Devtools, how can I continue after hitting a DOM breakpoint? 我可以浏览浏览器正在解析的所有JavaScript而不设置断点吗? - Can I step through all the javascript a browser is parsing without setting breakpoints?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM