admin管理员组

文章数量:1323707

I am just messing around and am logging out a div that I have selected with standard javascript but that I have executed inside a jQuery document.ready(fn) block.

$(document).ready(function(){
  console.log(document.getElementById( 'blah' ));
})

I'm really interested to know why sometimes I get...

<div id="blah"></div>

And other times I get...

Seems to log differently randomly.

I am just messing around and am logging out a div that I have selected with standard javascript but that I have executed inside a jQuery document.ready(fn) block.

$(document).ready(function(){
  console.log(document.getElementById( 'blah' ));
})

I'm really interested to know why sometimes I get...

<div id="blah"></div>

And other times I get...

Seems to log differently randomly.

Share Improve this question edited Dec 1, 2014 at 8:38 Chris Hayes 12.1k4 gold badges34 silver badges47 bronze badges asked Dec 1, 2014 at 5:01 ExitosExitos 29.8k39 gold badges128 silver badges178 bronze badges 4
  • 1 Can you provide the exact code and/or fiddles to demonstrate this behavior? Also are you seeing it consistently across multiple browsers? – 1252748 Commented Dec 1, 2014 at 5:10
  • I'm just writing it out using a console.log you can see it displayed... – Exitos Commented Dec 1, 2014 at 5:23
  • 1 You've got to tell us which web browser this question is about. – Chris Martin Commented Dec 1, 2014 at 5:28
  • I removed the jquery tag - this question would be exactly the same without jQuery. – Chris Hayes Commented Dec 1, 2014 at 5:44
Add a ment  | 

2 Answers 2

Reset to default 2

This does appear to be random, at least in Chrome. If you want to force it one way or another in the Chrome console you can use console.dir and console.dirxml.

  • console.dirxml will force the output to be like your first example, in xml format
  • console.dir will output like your second example, in object notation.

More examples of console mands in Chrome: https://developer.chrome./devtools/docs/console-api

Edit: dirxml also works in Internet Explorer 11 and later but not in Firefox though this shouldn't be an issue as Firefox outputs elements in XML format.

Each environment decides on how to present objects:

FireFox/Firebug:  <div id="blah">

IE/Developer tools:   [object HTMLDivElement] 

本文标签: