简体   繁体   English

我想在Chrome的控制台中显示对象的值

[英]I want to display an object's values in chrome's console

It's something I'm missing in Chrome's console. Chrome的控制台中缺少此功能。 I'm following a screencast where a javascript object is displayed in a browsable tre-like structure like this: 我正在进行一个截屏,其中一个javascript对象以类似tre的可浏览tre结构显示:

(arrow here) Object (此处为箭头)

When typing this for example, I'm only getting an empty array: 例如,当输入此内容时,我只会得到一个空数组:

jQuery() jQuery()

If you want to browse the elements within the jQuery object, you'll have to select something first. 如果要浏览jQuery对象中的元素,则必须先选择一些内容。
$() returns an empty jQuery object. $()返回一个空的jQuery对象。


If, on the other hand, you want to browse the methods/properties of the object created, you should use the console.dir method, which will give you a more in-depth view of your object: 另一方面,如果要浏览所创建对象的方法/属性,则应使用console.dir方法,该方法将为您提供对象的更深入视图:

console.dir(jQuery());

Here's the fiddle: http://jsfiddle.net/asYay/ 这是小提琴: http : //jsfiddle.net/asYay/

The Object you are getting by typing 通过键入获取的对象

jQuery() 

is an empty jQuery Object, which is represented by the same syntax as an empty array 是一个空的jQuery对象,用与空数组相同的语法表示

[]

jQuery Objects are Array-like objects of html elements you select from the body. jQuery对象是您从正文中选择的html元素的类似数组的对象。 In your case, the selector (the first argument of the function which you would normally write into the function) is empty, so there is nothing to search for for jQuery and it returns an empty object. 在您的情况下,选择器(通常会写入该函数的函数的第一个参数)为空,因此无需搜索jQuery,它会返回一个空对象。 If you would pass an argument with a DOM element (for example the body), it would return the body within the array 如果您传递带有DOM元素的参数(例如body),它将返回数组中的body

jQuery('body') //=> Array with the body html element inside

Note that HTML elements within the jQuery Object are normally not represented the same as standard objects in the Google Console. 请注意,jQuery对象中的HTML元素通常与Google控制台中的标准对象所表示的不同。 For standard objects, you will get the Object with its properties as a tree structure with an arrow before it to expand it (like the one you see in the screencast), with HTML elements, you get the DOM node , but no properties or methods to expand. 对于标准对象,您将获得带有其属性的对象,该对象的属性为带有箭头的树形结构,然后将其展开(如您在截屏视频中看到的那样),带有HTML元素的对象为DOM节点 ,但没有属性或方法扩张。

To see the difference, try this: 要查看区别,请尝试以下操作:

Display of an instance or a standard object in the Chrome console: 在Chrome控制台中显示实例或标准对象:

var object = {
  hi: 'im an object', 
  and_i: 'am represented as a tree like structure', 
  i_can_haz: function() { return 'this is great, it shows all the stuff' }
};

If you type again: 如果再次输入:

object

You will get the Chrome Console Object representation. 您将获得Chrome控制台对象表示形式。

For a HTML Object, just do this 对于HTML对象,只需执行此操作

var object = document.getElementsByTagName('body');

And if you want to access its properties and functions, use the dir method 如果要访问其属性和功能,请使用dir方法

dir(object);

You can use dir on virtually any object to access the properties 您几乎可以在任何对象上使用dir来访问属性

And object will be a representation of the body element. 对象将是主体元素的表示。 As said before, all jQuery does when selecting is putting these elements into an array, essentially. 如前所述,选择时所有jQuery所做的事情本质上就是将这些元素放入数组中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM