如何检查 jQuery 对象?
在 jQuery 1.4.4 中,如果我在 Google Chrome 的控制台中执行此操作:
var divs = $('div');
...我返回的似乎是 DOM 元素数组。但我知道它一定是一个 jQuery 对象,因为我可以在它上面链接 jQuery 方法:
divs.hide('slow').show('slow'); // etc
我想看到的是 jQuery 对象,带有列出其所有方法的 .fn
属性,等等。我'我很确定我以前能够看到这一点。
如果我创建自己的对象,如下所示:
var foo = {species: 'marmot', flavor: 'lemon'}
...我可以在控制台中深入了解它的属性。
如何在控制台中检查 jQuery 对象?
另外,到底做了什么魔法才能使它看起来像一个数组?
更新 - 它确实改变了
如果我加载旧版本的 jQuery - 例如,将其复制并粘贴到控制台的空白选项卡中:
http://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js
...而我然后执行以下操作:
var divs = $('div');
...我确实返回了 jQuery.fn.jQuery.init
,我可以在控制台中深入研究它。所以从那时起,事情肯定发生了变化。
In jQuery 1.4.4, if I do this in Google Chrome's console:
var divs = $('div');
... what I get back appears to be an array of DOM elements. But I know it must be a jQuery object, because I can chain jQuery methods on it:
divs.hide('slow').show('slow'); // etc
What I want to see is the jQuery object, with a .fn
property listing all its methods, etc. I'm pretty sure I used to be able to see this.
If I make my own object, like this:
var foo = {species: 'marmot', flavor: 'lemon'}
...I can dig into its properties in the console.
How can I inspect the jQuery object in the console?
Also, what magic is being done to make this look like an array?
Update - it did change
If I load an old version of jQuery - for example, copy and paste this into my console in a blank tab:
http://ajax.googleapis.com/ajax/libs/jquery/1.2.3/jquery.min.js
... and I then do this:
var divs = $('div');
... I do get back jQuery.fn.jQuery.init
, which I can dig into in the console. So something definitely changed since then.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我相信此网站描述了您正在寻找的内容详细但总结一下(来自链接):
并且:
$('div').toSource();
编辑: 仅适用于 FF应该是您想要显示对象属性的内容。
对于 Chrome:

基本上,您会转到 Chrome 中的 Javascript 控制台。单击“脚本”选项卡 (#1)。在您要检查的代码处放置一个断点 (#2)。然后运行脚本,当它在该位置中断时,检查范围变量 (#3)。特别是
__proto__
部分。I believe this site describes what you're looking for in detail but to summarize (from the link):
And:
$('div').toSource();
Edit: Only works in FFShould be what you want for showing the properties of the object.
For Chrome:

Basically, you go to the Javascript Console in Chrome. Click on the Scripts tab (#1). Put a breakpoint on the spot where the code is you want to check (#2). Then run the script and when it breaks on that spot, check the scope variables (#3). Specifically the
__proto__
section.这并不能以非常令人满意的方式回答您的问题,但它可能会帮助您,具体取决于您的需求:
我注意到,如果您使对象不那么“类似于数组”,那么 Chrome 会像它一样记录它非数组对象(即具有可扩展的属性树)。
使其不像数组的一种方法是为
length
属性指定一个非数字值:ps 您可能希望将对象的
length
设置回其原始值再次使用之前的值。This doesn't answer your question in a very satisfying way, but it may help you, depending on what you're after:
I noticed that if you make the object less "array-like", then Chrome logs it as it would for a non-array object (i.e. with an expandable tree of properties).
One way to make it less array-like is to give the
length
property a non-numeric value:p.s. You'd probably want to set the object's
length
back to its original value before using it again.我在网上发现了一次这个检查功能,然后就再也没有回头。虽然它不是 jQuery :/
console.log(obj) 也很酷,但我最近发现了另一个非常酷的函数。
尝试 console.dir(obj),然后在控制台中您将看到您的 obj 将是一个不错的小节点类型结构,您将能够查看所有深度级别。
尝试
I found this inspect function online one time and never looked back. It isn't jQuery though :/
Also console.log(obj) is cool, but I recently found another very cool function.
Try console.dir(obj), then in the console you will see that your obj will be a nice little node type structure which you will be able to look at all depth levels.
Try