理解querySelector & querySelectorAll 的控制台输出
谁/什么决定如何将对象
的内容写入控制台
?
有时,您会得到对象文字的可折叠表示,而有时它只是打印 DOM
结构。
给定这个 HTML:
<div class="container">
<video src="low.mp4"></video>
<video src="high.mp4"></video>
<video src="mega.mp4"></video>
</div>
如果我们使用以下命令点击 console
(WebKit/Firebug) 然后
> var firstVideo = document.querySelector('video')
:
> firstVideo
它返回:
<video src="low.mp4"></video>
同时,假设我们有另一个对象,例如:
var guitar = { name:'Stratocaster', strings:6 }
向控制台询问:
> guitar
给我们:
我已经多次遇到这种行为,但通常会通过其他方式回避我的好奇心。现在这有点痛苦,所以我正在进一步研究它。仅仅是因为所讨论的对象是一个 DOM 元素,而检查器只是在帮助我们吗?
(我意识到我的示例有点奇怪(比较 DOM
对象和文字),但这是说明问题的最简单/最快的方法。)
使用上一个问题中的一些信息,我可以得到关于每个有问题的对象的更多信息:
video.constructor
返回:
HTMLVideoElementConstructor
而:guitar.constructor
返回:
function Object() {
[native code]
}
为什么它很重要?
情况如下:
> firstVideo.parentElement
我无法查看该元素当我必须单独处理每个属性时(即,firstVideo.parentElement.offsetHeight),这真是太糟糕了。
那么,我的理解差距在哪里——实际上到底发生了什么?
Who/what decides how to write the contents of an object
to the console
?
Sometimes you'll get a collapsable representation of the object literal while other times it simply prints the DOM
structure.
Given this HTML:
<div class="container">
<video src="low.mp4"></video>
<video src="high.mp4"></video>
<video src="mega.mp4"></video>
</div>
If we hit the console
(WebKit/Firebug) with:
> var firstVideo = document.querySelector('video')
And then:
> firstVideo
It returns:
<video src="low.mp4"></video>
While, say we've got another object like:
var guitar = { name:'Stratocaster', strings:6 }
Asking the console for:
> guitar
Gives us:
I've run into the behavior a number of times but typically side-stepped my curiosity by some other means. Now it's sort of a pain so I'm looking into it some more. Is it simply because the object in question is a DOM
element and the inspector is just helping us out?
(I realize my example is a little odd (comparing a DOM
object and a literal), but it's the simplest/quickest way to illustrate the issue.)
Using some info from a previous question I could get a little more info about each object in question:
video.constructor
returns:
HTMLVideoElementConstructor
While: guitar.constructor
returns:
function Object() {
[native code]
}
Why does it matter?
Situations like:
> firstVideo.parentElement
I can't look at this element and inspect its properties as simply when I have to go after each one individually (i.e., firstVideo.parentElement.offsetHeight
), which is a total bummer.
So, where's the gap in my understanding--what's actually going on?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果结果是 DOM 元素,控制台会以不同的方式记录。它们输出元素的源 HTML(或 XML),而不是列出其属性。
-
querySelector
返回一个 DOM 元素,因此控制台显示该元素的来源。-
querySelectorAll
返回一个 DOM 元素数组,它应该正常显示一个数组。您始终可以使用 dir(object) 强制输出列表对象的属性。
您可以查看 Firebug 命令行 API。
The consoles log differently if the result is an DOM element. They output the source HTML (or XML) of the element instead of listing its properties.
-
querySelector
returns a DOM element, so the console shows the source of the element.-
querySelectorAll
returns an array of DOM elements, which should show an array as normal.You can always force the output list the properties of an object by using
dir(object)
.You can check out the Firebug Command Line API.
当您将 dom 节点(这就是您的情况,因为 querySelector 返回一个元素)记录到控制台时,您会得到它的字符串表示形式,因为它更清晰,并且它使您可以访问一些标准功能,例如如果您悬停日志消息或获取其如果您单击它,则将其定位到文档中。如果您想像其他对象一样记录 dom 节点以检查其属性,您可以使用console.dir(node)。
在另一种情况下,当您记录简单对象的构造函数和 dom 节点的构造函数时,结果是相同的(至少在 Firebug 中),因为您正在记录函数并且控制台打印其名称,即
HTMLVideoElementConstructor
用于视频元素,Object
用于简单对象。When you log a dom node (that is your situation because querySelector returns an element) into the console you get its string representation because it's more clear and it makes you access some standard functionality like highlight the node if you hover the log message or get its location into the document if you click it. If you want to log a dom node like every other object to inspect its properties you can use
console.dir(node)
.In the other case when you log the constructor of a simple object and the constructor of a dom node the result is the same (in Firebug at least) because you are logging a function and the console prints its name, that is
HTMLVideoElementConstructor
for video elements andObject
for simple objects.