Javascript 在警告框中转储 jquery 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8708814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
dump jquery object in an alert box
提问by planet x
I am not quite adept in maneuvering jQuery, and it came to a point that I need to debug a program that was passed down from me without a documentation.
我不太擅长操纵 jQuery,因此我需要调试一个没有文档的程序。
I have this var a, an object, that I really want to know the content of its collection. In my mind I need a function like foreach()
in PHP to iterate over this object variable. Upon researching I end up in using jQuery.each(). Now I can clearly iterate and see what was inside var a.
我有这个 var a对象,我真的很想知道它的集合的内容。在我看来,我需要一个像foreach()
PHP一样的函数来迭代这个对象变量。经过研究,我最终使用了jQuery.each()。现在我可以清楚地迭代并查看 var a 中的内容。
However, it was kind of annoying to alert once every value on the var a. What I wanna know if it's possible to display all the contents in just one pop of alert box?
但是,对 var a上的每个值发出一次警报有点烦人。我想知道是否可以在弹出的警告框中显示所有内容?
Here is my code:
这是我的代码:
$.each(a, function(index, value) {
alert(index + ': ' + value);
});
The var acontains infos such as:
var a包含以下信息:
creationdate: date_here
id: SWFUpload
modificationdate: date_here
type: .jpg
index: 0
name: uploaded_filename.jpg
size: size_in_bytes
BTW: The var ais called via file upload script.
顺便说一句: var a是通过文件上传脚本调用的。
回答by mgibsonbr
Why don't you just accumulate the values in an array, then display the whole array (for instance, using JSON)? Example:
为什么不将值累积在一个数组中,然后显示整个数组(例如,使用 JSON)?例子:
var acc = []
$.each(a, function(index, value) {
acc.push(index + ': ' + value);
});
alert(JSON.stringify(acc));
In any case, I'd suggest using a debug tool like Firebug. So you could just use console.log(a)and be able to navigate freely through the objects's fields.
无论如何,我建议使用像Firebug这样的调试工具。所以你可以只使用console.log(a)并且能够在对象的字段中自由导航。
回答by Sudhir Bastakoti
In firefox you could try:
在 Firefox 中,您可以尝试:
alert(yourObject.toSource());
OR you could use some plugin: See: jQuery Dump Plugin
或者你可以使用一些插件:参见:jQuery Dump Plugin