Javascript 显示对象内容 - JS/jQuery
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8143386/
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
Display object contents - JS/jQuery
提问by Matt
With $(this).data("events");
returning [object Object]
, I need to see what's actually going on in there. I found this:
随着$(this).data("events");
返回[object Object]
,我需要看看那里到底发生了什么。我找到了这个:
var Finder = "";
$.each($(this).data("events"), function(i, n){
Finder += "Name: " + i + ", Value: " + n + " | ";
});
However, n
still returns [object Object]
:
但是,n
仍然返回[object Object]
:
EDIT: (Output) --
编辑:(输出)-
Name: click, Value: [object Object] |
--
——
Is there an efficient way to show everything inside that sucker, kind of like print_r
in PHP?
有没有一种有效的方法来显示那个傻瓜里面的所有东西,有点像print_r
在 PHP 中?
回答by ipr101
console.log($(this).data("events"))
in Chrome (or other browsers) would allow you to drill into the object.
console.log($(this).data("events"))
在 Chrome(或其他浏览器)中将允许您钻取对象。
Ctrl+Shift+J gets you to the console in Chrome.
Ctrl+Shift+J 将您带到 Chrome 中的控制台。
回答by Jasper
You can use .toSource()
to turn JavaScript objects into a string representation that you can view without a nice error console like in Firebug or Chrome Dev. Tools:
您可以使用.toSource()
将 JavaScript 对象转换为字符串表示形式,无需像 Firebug 或 Chrome Dev 那样的良好错误控制台即可查看该字符串表示形式。工具:
alert($(this).data("events").toSource());
回答by leo.vingi
If you can't use console.log
then alert( $(this).data("events").toSource() )
can also be used.
如果你不能使用console.log
那么alert( $(this).data("events").toSource() )
也可以使用。
回答by Nikunj K.
Print content of object you can use
打印您可以使用的对象的内容
console.log(obj_str);
you can see the result in console like below.
您可以在控制台中看到结果,如下所示。
Object {description: "test"}
For open console press F12 in chrome browser, you will found console tab in debug mode.
对于在 Chrome 浏览器中打开控制台按 F12,您将在调试模式下找到控制台选项卡。