Chrome-Console( Command Line API Reference)
来源于:https://developers.google.com/web/tools/chrome-devtools/console/command-line-reference
The Command Line API contains a collection of convenience functions for performing common tasks: selecting and inspecting DOM elements, displaying data in readable format, stopping and starting the profiler, and monitoring DOM events.
Note: This API is only available from within the console itself. You cannot access the Command Line API from scripts on the page.
$_
$_
returns the value of the most recently evaluated expression.
In the following example, a simple expression (2 + 2
) is evaluated. The $_
property is then evaluated, which contains the same value:
In the next example, the evaluated expression initially contains an array of names. Evaluating $_.length
to find the length of the array, the value stored in $_
changes to become the latest evaluated expression, 4:
$0 - $4
The $0
, $1
, $2
, $3
and $4
commands work as a historical reference to the last five DOM elements inspected within the Elements panel or the last five JavaScript heap objects selected in the Profiles panel. $0
returns the most recently selected element or JavaScript object, $1
returns the second most recently selected one, and so on.
In the following example, an element with the class medium
is selected in the Elements panel. In the Console drawer, $0
has been evaluated and displays the same element:
The image below shows a different element selected in the same page. The $0
now refers to newly selected element, while $1
returns the previously selected one:
$(selector)
$(selector)
returns the reference to the first DOM element with the specified CSS selector. This function is an alias for the document.querySelector() function.
The following example returns a reference to the first <img>
element in the document:
Right-click on the returned result and select 'Reveal in Elements Panel' to find it in the DOM, or 'Scroll in to View' to show it on the page.
The following example returns a reference to the currently selected element and displays its src property:
Note: If you are using a library such as jQuery that uses $
, this functionality will be overwritten, and $
will correspond to that library's implementation.
$$(selector)
$$(selector)
returns an array of elements that match the given CSS selector. This command is equivalent to calling document.querySelectorAll().
The following example uses $$()
to create an array of all <img>
elements in the current document and displays the value of each element's src
property:
var images = $$('img');
for (each in images) {
console.log(images[each].src);
}
Note: Press Shift + Enter in the console to start a new line without executing the script.
$x(path)
$x(path)
returns an array of DOM elements that match the given XPath expression.
For example, the following returns all the <p>
elements on the page:
$x("//p")
The following example returns all the <p>
elements that contain <a>
elements:
$x("//p[a]")
clear()
clear()
clears the console of its history.
clear();
copy(object)
copy(object)
copies a string representation of the specified object to the clipboard.
copy($0);
debug(function)
When the specified function is called, the debugger is invoked and breaks inside the function on the Sources panel allowing to step through the code and debug it.
debug(getData);
Use undebug(fn)
to stop breaking on the function, or use the UI to disable all breakpoints.
For more information on breakpoints, see Debug with Breakpoints.
dir(object)
dir(object)
displays an object-style listing of all the specified object's properties. This method is an alias for the Console API's console.dir()
method.
The following example shows the difference between evaluating document.body
directly in the command line, and using dir()
to display the same element:
document.body;
dir(document.body);
For more information, see the console.dir()
entry in the Console API.
dirxml(object)
dirxml(object)
prints an XML representation of the specified object, as seen in the Elements tab. This method is equivalent to the console.dirxml() method.
inspect(object/function)
inspect(object/function)
opens and selects the specified element or object in the appropriate panel: either the Elements panel for DOM elements or the Profiles panel for JavaScript heap objects.
The following example opens the document.body
in the Elements panel:
inspect(document.body);
When passing a function to inspect, the function opens the document up in the Sources panel for you to inspect.
getEventListeners(object)
getEventListeners(object)
returns the event listeners registered on the specified object. The return value is an object that contains an array for each registered event type ("click" or "keydown", for example). The members of each array are objects that describe the listener registered for each type. For example, the following lists all the event listeners registered on the document object:
getEventListeners(document);
If more than one listener is registered on the specified object, then the array contains a member for each listener. In the following example, there are two event listeners registered on the #scrollingList element for the "mousedown" event:
You can further expand each of these objects to explore their properties:
keys(object)
keys(object)
returns an array containing the names of the properties belonging to the specified object. To get the associated values of the same properties, use values()
.
For example, suppose your application defined the following object:
var player1 = { "name": "Ted", "level": 42 }
Assuming player1
was defined in the global namespace (for simplicity), typing keys(player1)
and values(player1)
in the console results in the following:
monitor(function)
When the function specified is called, a message is logged to the console that indicates the function name along with the arguments that are passed to the function when it was called.
function sum(x, y) {
return x + y;
}
monitor(sum);
Use unmonitor(function)
to cease monitoring.
monitorEvents(object[, events])
When one of the specified events occurs on the specified object, the Event object is logged to the console. You can specify a single event to monitor, an array of events, or one of the generic events "types" mapped to a predefined collection of events. See examples below.
The following monitors all resize events on the window object.
monitorEvents(window, "resize");
The following defines an array to monitor both "resize" and "scroll" events on the window object:
monitorEvents(window, ["resize", "scroll"])
You can also specify one of the available event "types", strings that map to predefined sets of events. The table below lists the available event types and their associated event mappings:
Event type & Corresponding mapped events | |
---|---|
mouse | "mousedown", "mouseup", "click", "dblclick", "mousemove", "mouseover", "mouseout", "mousewheel" |
key | "keydown", "keyup", "keypress", "textInput" |
touch | "touchstart", "touchmove", "touchend", "touchcancel" |
control | "resize", "scroll", "zoom", "focus", "blur", "select", "change", "submit", "reset" |
For example, the following uses the "key" event type all corresponding key events on an input text field currently selected in the Elements panel.
monitorEvents($0, "key");
Below is sample output after typing a characters in the text field:
profile([name]) and profileEnd([name])
profile()
starts a JavaScript CPU profiling session with an optional name. profileEnd()
completes the profile and displays the results in the Profile panel. (See also Speed Up JavaScript Execution.)
To start profiling:
profile("My profile")
To stop profiling and display the results in the Profiles panel:
profileEnd("My profile")
Profiles can also be nested. For example, this will work in any order:
profile('A');
profile('B');
profileEnd('A');
profileEnd('B');
Result in the profiles panel:
Note: Multiple CPU profiles can operate at once and you aren't required to close them out in creation order.
table(data[, columns])
Log object data with table formatting by passing in a data object in with optional column headings. For example, to display a list of names using a table in the console, you would do:
var names = {
0: { firstName: "John", lastName: "Smith" },
1: { firstName: "Jane", lastName: "Doe" }
};
table(names);
undebug(function)
undebug(function)
stops the debugging of the specified function so that when the function is called, the debugger is no longer invoked.
undebug(getData);
unmonitor(function)
unmonitor(function)
stops the monitoring of the specified function. This is used in concert with monitor(fn)
.
unmonitor(getData);
unmonitorEvents(object[, events])
unmonitorEvents(object[, events])
stops monitoring events for the specified object and events. For example, the following stops all event monitoring on the window object:
unmonitorEvents(window);
You can also selectively stop monitoring specific events on an object. For example, the following code starts monitoring all mouse events on the currently selected element, and then stops monitoring "mousemove" events (perhaps to reduce noise in the console output):
monitorEvents($0, "mouse");
unmonitorEvents($0, "mousemove");
values(object)
values(object)
returns an array containing the values of all properties belonging to the specified object.
values(object);
Chrome-Console( Command Line API Reference)的更多相关文章
- Chrome console & Command Line API
Chrome console & Command Line API $ && $$ querySelector querySelectorAll Command Line AP ...
- 如何在Windows下开发Python:在cmd下运行Python脚本+如何使用Python Shell(command line模式和GUI模式)+如何使用Python IDE
http://www.crifan.com/how_to_do_python_development_under_windows_environment/ 本文目的 希望对于,如何在Windows下, ...
- mysql 乱码问题(程序界面显示正常,mysql command line显示乱码)
今天用java写一个程序,用的是mysql数据库.界面出现乱码,然后写了一个过滤器结果了乱码问题. 但是,当我在mysql command line 中查询数据的时候,在界面上显示正常的数据,在mys ...
- Java 9 揭秘(12. Process API 更新)
Tips 做一个终身学习的人. 在本章中,主要介绍以下内容: Process API是什么 如何创建本地进程 如何获取新进程的信息 如何获取当前进程的信息 如何获取所有系统进程的信息 如何设置创建,查 ...
- Java 9 揭秘(13. Collection API 更新)
Tips 做一个终身学习的人. 在本章中,主要介绍以下内容: 在JDK 9之前如何创建了不可变的list,set和map以及使用它们的问题. 如何使用JDK 9中的List接口的of()静态工厂方法创 ...
- 获取bing图片并自动设置为电脑桌面背景(使用 URLDownloadToFile API函数)
众所周知,bing搜索网站首页每日会更新一张图片,张张漂亮(额,也有一些不合我口味的),特别适合用来做电脑壁纸. 我们想要将bing网站背景图片设置为电脑桌面背景的通常做法是: 上网,搜索bing 找 ...
- Python -- Scrapy 命令行工具(command line tools)
结合scrapy 官方文档,进行学习,并整理了部分自己学习实践的内容 Scrapy是通过 scrapy 命令行工具进行控制的. 这里我们称之为 “Scrapy tool” 以用来和子命令进行区分. 对 ...
- Spring-boot在windows上安装CLI(Command Line Interface)的步骤!
首先去下载安装包,我这里整了一个zip包,一个tar包,下载地址:https://github.com/zhangyawei117/Spring-boot-CLI.git 下载完了之后,把zip包解压 ...
- 使用python(command line)出现的ImportError: No module named 'xxx'问题
当你在python.exe直接输入 import test 时报出importerror: no module named 'test' ,这个错误时由于路径问题,sys并没有找到你输入的这个文件 解 ...
随机推荐
- ios textfield / textview长按复制粘贴中文显示
当我们在写应用时要复制粘贴文本框内容时,默认显示的文字为英文字体,可按如下步骤设置,显示中文:
- JAVA匿名内部类
首先定义一个抽象类Computer public abstract class Computer { //抽象类是不可以常见对象的 int a=1; public abstract void onli ...
- fragment 监听返回
@Override public void onResume() { super.onResume(); getView().setFocusableInTouchMode(true); getVie ...
- Java异常之自定义异常
哎呀,妈呀,又出异常了!俗话说:"代码虐我千百遍,我待代码如初恋". 小Alan最近一直在忙着工作,已经很久没有写写东西来加深自己的理解了,今天来跟大家聊聊Java异常.Java异 ...
- #研发解决方案介绍#Tracing(鹰眼)
郑昀 最后更新于2014/11/12 关键词:GoogleDapper.分布式跟踪.鹰眼.Tracing.HBase.HDFS. 本文档适用人员:研发 分布式系统为什么需要 Tracing? ...
- Microsoft SQL Server 2005 Service fails to start
今天碰到一雷死人的事情,在Windows Server 2012 R2上安装SQL SERVER 2005标准版过程中一直遇到"The SQL Server service failed t ...
- phpAdmin安装
phpAdmin是和Navicat重复的功能 负责管理MySql数据库 不过他是使用浏览器进行管理MySql数据库 PHP环境搭建的完整步骤 http://www.cnblogs.com/azhe-s ...
- 问题解决——MFC Ribbon 添加图标
=================================版权声明================================= 版权声明:本文为博主原创文章 未经许可不得转载 请通过右 ...
- Linux From Scratch(从零开始构建Linux系统,简称LFS)- Version 7.7(一)
一. 准备工作 1. 需要一个Linux宿主系统,例如早先版本的 LFS,Ubuntu/Fedora,SuSE 或者是在你的架构上可以运行的其它发行版 如果想实现Win7与Linux双系统,可参考我的 ...
- 萌新笔记——C++里将string类字符串(utf-8编码)分解成单个字(可中英混输)
最近在建词典,使用Trie字典树,需要把字符串分解成单个字.由于传入的字符串中可能包含中文或者英文,它们的字节数并不相同.一开始天真地认为中文就是两个字节,于是很happy地直接判断当前位置的字符的A ...