php jQuery:print_r() 显示等效?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/456646/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 22:49:07  来源:igfitidea点击:

jQuery: print_r() display equivalent?

phpjquerydebugging

提问by Eli

Possible Duplicate:
JavaScript data formatting/pretty printer

可能的重复:
JavaScript 数据格式/漂亮的打印机

I am getting a bit tired of looking at unformatted json blobs in FireBug.

我有点厌倦了在 FireBug 中查看未格式化的 json blob。

Does anyone know an equivalent to PHP's print_r() for jQuery?

有没有人知道 jQuery 的 PHP 的 print_r() 等价物?

Something that would recursively make a display string from an object or array, that I could display on the page for quick debugging?

可以从对象或数组递归生成显示字符串的东西,我可以在页面上显示以进行快速调试?

Thanks!

谢谢!

回答by Paolo Bergantino

console.logis what I most often use when debugging.

console.log是我调试时最常使用的。

I was able to find this jQuery extensionthough.

不过我还是找到了这个jQuery extension

回答by CMS

You could use very easily reflectionto list all properties, methods and values.

您可以非常轻松地使用反射来列出所有属性、方法和值。

For Gecko based browsers you can use the .toSource() method:

对于基于 Gecko 的浏览器,您可以使用 .toSource() 方法:

var data = new Object();
data["firstname"] = "John";
data["lastname"] = "Smith";
data["age"] = 21;

alert(data.toSource()); //Will return "({firstname:"John", lastname:"Smith", age:21})"

But since you use Firebug, why not just use console.log?

但是既然你使用 Firebug,为什么不直接使用 console.log 呢?

回答by Magnus Andersson

How about something like:

怎么样:

<script src='http://code.jquery.com/jquery-latest.js'></script>

function print_r(o){
return JSON.stringify(o,null,'\t').replace(/\n/g,'<br>').replace(/\t/g,'&nbsp;&nbsp;&nbsp;'); }

回答by Bill Zeller

You can also do

你也可以这样做

console.log("a = %o, b = %o", a, b);

where a and b are objects.

其中 a 和 b 是对象。

回答by Tomas

I've made a jQuery plugin for the equivalent of

我制作了一个 jQuery 插件,相当于

<pre>
<?php echo print_r($data) ?>
</pre>

You can download it at https://github.com/tomasvanrijsse/jQuery.dump

你可以在https://github.com/tomasvanrijsse/jQuery.dump下载

回答by Gogol

$.each(myobject, function(key, element) {
    alert('key: ' + key + '\n' + 'value: ' + element);
});

This does the work for me. :)

这对我有用。:)

回答by jerclarke

Top comment has a broken link to the console.log documentation for Firebug, so here is a link to the wiki article about Console. I started using it and am quite satisfied with it as an alternative to PHP's print_r().

热门评论有一个指向 Firebug 的 console.log 文档的断开链接,所以这里有一个指向关于 Console 的 wiki 文章的链接。我开始使用它并且对它作为 PHP 的 print_r() 的替代品感到非常满意。

Also of note is that Firebug gives you access to returned JSON objects even without you manually logging them:

另外值得注意的是,即使您不手动记录它们,Firebug 也可以让您访问返回的 JSON 对象:

  • In the console you can see the url of the AJAX response.
  • Click the triangle to expand the response and see details.
  • Click the JSON tab in the details.
  • You will see the response data organized with expansion triangles.
  • 在控制台中,您可以看到 AJAX 响应的 URL。
  • 单击三角形以展开响应并查看详细信息。
  • 单击详细信息中的 JSON 选项卡。
  • 您将看到用扩展三角形组织的响应数据。

This method take a couple more clicks to get at the data but doesn't require any additions in your actual javascript and doesn't shift your focus in Firebug out of the console (using console.log creates a link to the DOM section of firebug, forcing you to click back to console after).

此方法需要多次点击才能获取数据,但不需要在实际 javascript 中添加任何内容,并且不会将 Firebug 中的焦点移出控制台(使用 console.log 创建指向 firebug DOM 部分的链接,迫使您在之后单击返回控制台)。

For my money I'd rather click a couple more times when I want to inspect rather than mess around with the log, especially since keeps the console neat by not adding any additional cruft.

为了我的钱,当我想检查而不是弄乱日志时,我宁愿多点击几次,尤其是因为不添加任何额外的杂物来保持控制台整洁。

回答by niofox

Look at this: http://phpjs.org/functions/indexand find for print_r or use console.log() with firebug.

看看这个:http: //phpjs.org/functions/index并找到 print_r 或使用带有 firebug 的 console.log()。