zoukankan      html  css  js  c++  java
  • javascript仿php的print_r函数输出json数据【转】

        function print_r(theObj) {
            var retStr = '';
            if (typeof theObj == 'object') {
                retStr += '<div style="font-family:Tahoma; font-size:7pt;">';
                for (var p in theObj) {
                    if (typeof theObj[p] == 'object') {
                        retStr += '<div><b>['+p+'] => ' + typeof(theObj) + '</b></div>';
                        retStr += '<div style="padding-left:25px;">' + print_r(theObj[p]) + '</div>';
                    } else {
                        retStr += '<div>['+p+'] => <b>' + theObj[p] + '</b></div>';
                    }
                }
                retStr += '</div>';
            }
            return retStr;
        }

    在需要使用的地方调用这个函数就行啦。

    若还使用Jquery的话,可以将它做成Jquery的一个插件。

        (function($){
            $.fn.print_r = function(json){
                return $(this).each(function(e){
                    $(this).html(_print_r(json));
                })
            }
            function _print_r(theObj) {
                var retStr = '';
                if (typeof theObj == 'object') {
                    retStr += '<div style="font-size:12px;">';
                    for (var p in theObj) {
                        if (typeof theObj[p] == 'object') {
                            retStr += '<div><b>['+p+'] => ' + typeof(theObj) + '</b></div>';
                            retStr += '<div style="padding-left:25px;">' + _print_r(theObj[p]) + '</div>';
                        } else {
                            retStr += '<div>['+p+'] => <b>' + theObj[p] + '</b></div>';
                        }
                    }
                    retStr += '</div>';
                }
                return retStr;
            }   
            $.print_r = function(json){
                return _print_r(json);
            }
        })(jQuery);
  • 相关阅读:
    [转]男人25岁之前应该懂得的21个道理
    family album U.S.A 05
    Humour
    Pretty Boy
    [转]男人本色.女人风韵
    电话英语
    搞不懂的女人
    family album U.S.A 03
    [转]The flame of love(爱的火焰)
    四季花开!
  • 原文地址:https://www.cnblogs.com/hongchenok/p/2982959.html
Copyright © 2011-2022 走看看