工欲善其事必先利其器javascript
Chrome inspector -> Sources tab -> {}
复制代码
控制台支持一些变量和函数来选择DOM元素:java
$() 至关于document.querySelector()
// 返回第一个与之匹配的CSS选择器的元素
// 例如:$('div')它将返回本页的第一个div元素
$$() 至关于document.querySelectorAll()
// 返回一个数组,里面是与之匹配的CSS选择器的元素
$0~$4 依次返回五个最近你在元素面板选择过的DOM元素的历史记录
$0是最新的记录,以此类推
$_ 获取上次表达式结果
复制代码
function add(a,b){return a+b}
monitor(add)
add(1,2)
// output:
// function add called with arguments: 1, 2
// 3
复制代码
// 单个事件
monitorEvents(window, "resize")
resize Event {isTrusted: true, type: "resize", target: Window, currentTarget: Window, eventPhase: 2, …}
// 多个事件
monitorEvents($0, ['mousedown', 'mouseup'])
mousedown MouseEvent {isTrusted: true, screenX: -1034, screenY: 94, clientX: 28, clientY: 674, …}
// 全部事件
monitorEvents($0)
// 取消事件
unmonitorEvents($0)
复制代码
keys(console) //查看全部的console方法
["debug", "error", "info", "log", "warn", "dir", "dirxml", "table",
"trace", "group", "groupCollapsed", "groupEnd", "clear", "count",
"countReset", "assert", "profile", "profileEnd", "time", "timeLog",
"timeEnd", "timeStamp", "context", "memory"]
复制代码
let startTime = new Date().getTime();
console.log("started");
setTimeout(() => {
console.log(
"ended in " + (new Date().getTime() - startTime) + " milliseconds"
);
}, 3000);
// output:
// "started"
// "ended in 3001 milliseconds"
复制代码
console.time('Call to doSomething took')
doSomething()
console.timeEnd('Call to doSomething took')
// output:
// Call to doSomething took:300ms
复制代码
var t0 = performance.now();
doSomething();
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.");
// output:
// Call to doSomething took 300 milliseconds
复制代码
设置 -> Show timestampsweb
console -> 设置 -> 勾选 preserve log
chrome
document.designMode = "on"
复制代码