Chrome开发者工具详解(1)
id="iframe_0.9280889915856889" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22https://img.shields.io/badge/version-v1.0.0-blue.svg?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9280889915856889',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 95px; height: 20px;">
Chrome开发者工具面板
面板上包含了Elements、Console、Sources、Network、Timeline、Profiles、Application、Security、Audits这些功能按钮。
id="iframe_0.45542593781902396" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/chrome-devtools-panes.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.45542593781902396',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 1021px; height: 378px;">
这些按钮的功能点如下:
- Elements:查找网页源代码HTML中的任一元素,手动修改任一元素的属性和样式且能实时在浏览器里面得到反馈。
- Console:记录开发者开发过程中的日志信息,且可以作为与JS进行交互的命令行Shell。
- Sources:断点调试JS。
- Network:从发起网页页面请求Request后分析HTTP请求后得到的各个请求资源信息(包括状态、资源类型、大小、所用时间等),可以根据这个进行网络性能优化。
- Timeline:记录并分析在网站的生命周期内所发生的各类事件,以此可以提高网页的运行时间的性能。
- Profiles:如果你需要Timeline所能提供的更多信息时,可以尝试一下Profiles,比如记录JS CPU执行时间细节、显示JS对象和相关的DOM节点的内存消耗、记录内存的分配细节。
- Application:记录网站加载的所有资源信息,包括存储数据(Local Storage、Session Storage、IndexedDB、Web SQL、Cookies)、缓存数据、字体、图片、脚本、样式表等。
- Security:判断当前网页是否安全。
-
Audits:对当前网页进行网络利用情况、网页性能方面的诊断,并给出一些优化建议。比如列出所有没有用到的CSS文件等。
注: 这一篇主要讲解前三个面板Elements、Console、Sources。
Elements面板
实时编辑DOM节点和CSS样式
- 双击DOM树视图里面的节点,可以实时编辑标签属性,修改的效果会立刻反应在浏览器里面
id="iframe_0.9356192782656205" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/element1.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9356192782656205',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 1021px; height: 314px;">
- 点击右侧Style面板,可以实时修改CSS的属性值,这里面的所有样式Name和Value都是可以编辑的;在每个属性后面单击可以添加新的样式,如下图:
id="iframe_0.49002432868194057" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/element2.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.49002432868194057',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 1012px; height: 900px;">
- 点击右侧Computed面板,可以编辑左侧选中的盒子模型参数,所有的值都是可以修改的;点击不同的位置(top、bottom、left、right) 就可以修改元素的padding、border、margin属性值。
id="iframe_0.35092392160982433" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/element-computed.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.35092392160982433',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 1021px; height: 343px;">
- 查看网页的本地修改历史
- 点击Styles面板中修改过属性的文件名,会跳转到Source面板
- 在文件位置右击选择Local modifications,可以查看本地的所有修改记录
- 点击指定的时间点可以看到粉红背景的删除内容和绿色背景的添加内容
id="iframe_0.8664485256452648" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/source1.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8664485256452648',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 1014px; height: 844px;">
Console面板
控制台输出日志
通过JS代码或者命令行console.log()
、console.warn()
、console.error()
可以将日志信息输出到控制台
console.log
显示一般的基本日志信息,当要显示的基本日志太多时可以使用console.group
将相关的日志进行分组console.warn
显示带有黄色小图标的警告信息console.error
显示带有红色小图标的红色的错误信息
id="iframe_0.6617346826759642" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/console5.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6617346826759642',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 665px; height: 305px;">
console.assert
当第一个参数为false时,才会显示第一个参数的值
id="iframe_0.8017542371628239" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/console4.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.8017542371628239',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 742px; height: 215px;">
- 可以根据JS条件判断输出不同的日志信息
注: 当需要换到下一行而不是回车的时候,请按Shift
+Enter
。
id="iframe_0.2501400864693424" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/console3.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.2501400864693424',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 800px; height: 575px;">
控制台交互
- JS表达式计算
在上一小节,我们已经看到可以在控制台输入JS表达式点击Enter
即可得到表达式的值,当你在控制台输入命令时,会弹出相应的智能提示框,你可以用Tab
自动完成当前的建议项
- 选择元素
id="iframe_0.1590492469814866" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/console6-select.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.1590492469814866',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 522px; height: 275px;">
快捷方式 | 描述 |
---|---|
$() |
返回与指定的CSS选择器相匹配的第一个元素,等同于document.querySelector() |
$$() |
返回与指定的CSS选择器相匹配的所有元素的数组,等同于document.querySelectorAll() |
$x() | 返回与指定的XPath相匹配的所有元素的数组 |
id="iframe_0.08117928390946383" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/console-select-element-issue.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.08117928390946383',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 1021px; height: 537px;">
注: 我在实际操作过程中发现$()
并没有按预期返回相匹配的第一个元素,而是返回了所有匹配的元素数组,我也给Google提供了这个issue,等待Google的答复。
Sources面板
你可以在这个面板里面调试你的JS代码,也可以在工作区打开你的本地文件。
调试JS代码
- 你可以点击JS代码块前面的数字外来设置断点,如果当前代码是经过压缩的话,可以点击下方的花括号
{}
来增强可读性,所有的断点都会列出右侧的断点区。
id="iframe_0.13468816441991271" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/sources1.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.13468816441991271',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 1021px; height: 366px;">
- 设置断点
断点可以在DOM元素节点发生改变时、XHR生命周期状态改变时、指定的事件执行时被触发
① DOM元素节点发生改变时
在Elements面板中指定的DOM节点上右击,在弹出的菜单中选择Break
on...
,可以看到三个选择项,比如我们选择Subtree
modifications
,
那么当选择的节点里面的子节点被添加、删除、修改,则断点就会被触发。设置方式如下图:
id="iframe_0.9526800104645603" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/sources-element-debug2.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.9526800104645603',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 1021px; height: 592px;">
下图是在我的系统里添加指定省市指定医院时由于增加了元素节点而触发的断点,通过单步调试可以看到会弹出一个div
对话框供用户添加数据。
id="iframe_0.6571726912056342" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/sources-dom-changes.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.6571726912056342',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 1021px; height: 268px;">
② XHR生命周期状态改变时
当XHR生命周期状态发生改变或者XHR的URL与Sources面板右侧的XHR Breakpoints栏设置的字符串匹配时,则断点就会有触发。
id="iframe_0.41318329576561785" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/sources-xhr.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.41318329576561785',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 1021px; height: 357px;">
③ 指定的事件执行时
在Sources面板右侧的XHR Breakpoints栏下面是Event Listener Breakpoints,列出了各种类型的事件,勾选你要监听的事件,
在指定的事件执行时,断点就会有触发。
id="iframe_0.5322379181985639" src="data:text/html;charset=utf8,%3Cimg%20id=%22img%22%20src=%22http://odfx4jo3a.bkt.clouddn.com/event-listener.png?_=5948448%22%20style=%22border:none;max-width:1021px%22%3E%3Cscript%3Ewindow.onload%20=%20function%20()%20%7Bvar%20img%20=%20document.getElementById('img');%20window.parent.postMessage(%7BiframeId:'iframe_0.5322379181985639',width:img.width,height:img.height%7D,%20'http://www.cnblogs.com');%7D%3C/script%3E" frameborder="0" scrolling="no" style="margin: 0px; padding: 0px; border-width: initial; border-style: none; width: 607px; height: 720px;">