这让我抓狂了一段时间。当我 console.log 一个 DOM 节点(通过示例从 document.getElementById
返回)时,它显示为一个交互式 html 元素,就像它出现在“元素”选项卡上一样。
这肯定很方便,但有时我只想展开对象并查看其所有属性,就像我可以对登录到控制台的所有其他类型的对象一样。
有什么方法可以让 DOM 节点作为常规对象显示在控制台中?
最佳答案
使用 console.dir
而不是 console.log
。
console.log( document.body );
<body class="question-page">...</body>
console.dir( document.body );
> HTMLBodyElement
aLink: ""
attributes: NamedNodeMap
background: ""
...
console.dir(object)
Prints an interactive listing of all properties of the object. This looks identical to the view that you would see in the DOM tab.
Chrome 也支持 console.dir
,as shown here (未显示为 console
的属性,但它可用)。
关于javascript - Safari/WebKit 的 console.log DOM 节点作为对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7756340/