javascript - 不同格式的 Chrome 控制台日志记录元素
问题描述
当我使用 Chrome 开发工具来控制台记录一个元素时,它会以两种不同的格式打印。当我刷新浏览器时,它会在两种格式之间随机切换。
<h1 class="heading">Heading</h1>
<script>
console.log(document.querySelector(".heading"));
</script>
所需格式:https ://i.stack.imgur.com/XV7fB.png
为什么会这样?https://i.stack.imgur.com/YlEGQ.png
结束了包装日志功能:
function log(value) {
window.addEventListener("load", function () {
console.log(value);
});
}
解决方案
不知何故,您的浏览器console.log()
与console.dir()
. 在此处阅读不同之处。
对于这两种情况,我在我的机器上得到的输出 -
> console.log(document.querySelector(".heading"));
<h1 class="heading">Heading</h1>
> console.dir(document.querySelector(".heading"));
h1.heading