0

运行此代码在 Chrome javascript 控制台中交替显示<div id="c"></div>扩展。 HTMLDivElement

$(function() {
    console.log( $("#c")[0] );
});

<div id="c"></div>

如果我运行alert()而不是console.log(),它总是返回 [object HTMLDivElement]。

并不是说它让我很不安,但是如果输入没有改变,这种输出不应该有点恒定吗?那么这是Chrome控制台如何呈现对象的某种错误还是什么?

顺便说一句,我无法通过 jsfiddle 重现该问题。我的 Chrome 是 21.0.1180.89 版和 jQuery - 1.6.4 版。


编辑:示例:我刷新页面,控制台显示<div id="c"></div>. 然后我再次刷新页面,控制台现在显示可扩展HTMLDivElement等等。

4

3 回答 3

3

console.log has the ability to show you the object. alert shows strings, numbers, primitive types, you pass an object and alert will say that is an object. because the tostring() of object returns [object HTMLDivElement] in your case.

于 2012-09-14T16:20:48.733 回答
1

In Chrome console the $ is already a reference for document.getElementById(), so try to use jQuery instead of $

jQuery(function() {
    console.log( jQuery("#c")[0] );
});

This should solve the issue, giving a uniform output. And as others answered, the alert is just applying a toString() method (since alert function is expecting to output a string).

于 2012-09-14T16:19:27.320 回答
0

JavaScript 警报向您显示对象的 toString() 结果,因为它不能显示字符串以外的任何内容。控制台能够为您提供对象的交互式表示。

于 2012-09-14T16:25:05.873 回答