复制代码 代码如下:
<script type="text/javascript">
<!--
var my={
str:'',
deep:0,
block:' ',
get_pre:function(n)
{
pre='';
for(i=0;i<n;i++)
{
pre+=this.block;
}
return pre;
},
show_obj:function(obj)
{
for(k in obj)
{
if(typeof(obj[k])!='object' && typeof(obj[k])!='array')
{
pre=this.get_pre(this.deep);
this.str+=pre+k+'=>'+obj[k]+'\n';
}
else if(typeof(obj[k])=='object' && typeof(obj[k].length)=='undefined')//如果是对象
{
pre=this.get_pre(this.deep);
this.str+=pre+k+'=>OBJECT{\n';
this.deep++;//开始递归,深度+1
this.show_obj(obj[k]);
pre = this.get_pre(this.deep);
this.deep--;//递归结束一个 深度-1
this.str+=pre+'}\n';
}
else if(typeof(obj[k])=='object' && typeof(obj[k].length)!='undefined')//如果是数组
{
pre=this.get_pre(this.deep);
this.str+=pre+k+'=>ARRAY[\n';
this.deep++;//同对象
this.show_obj(obj[k]);
pre = this.get_pre(this.deep);
this.deep--;//同对象
this.str+=pre+']\n';
}
}
return this.str;
},
alert_obj:function(obj)
{
alert(this.show_obj(obj))
}
}
my.alert_obj({a:{b:{c:{d:'hello world'}}}});
//-->
</script>
chrome 可以用 console.log
ie的话,
<script type="text/javascript">
<!--
var my={
str:'',
deep:0,
block:' ',
get_pre:function(n)
{
pre='';
for(i=0;i<n;i++)
{
pre+=this.block;
}
return pre;
},
show_obj:function(obj)
{
for(k in obj)
{
if(typeof(obj[k])!='object' && typeof(obj[k])!='array')
{
pre=this.get_pre(this.deep);
this.str+=pre+k+'=>'+obj[k]+'\n';
}
else if(typeof(obj[k])=='object' && typeof(obj[k].length)=='undefined')//如果是对象
{
pre=this.get_pre(this.deep);
this.str+=pre+k+'=>OBJECT{\n';
this.deep++;//开始递归,深度+1
this.show_obj(obj[k]);
pre = this.get_pre(this.deep);
this.deep--;//递归结束一个 深度-1
this.str+=pre+'}\n';
}
else if(typeof(obj[k])=='object' && typeof(obj[k].length)!='undefined')//如果是数组
{
pre=this.get_pre(this.deep);
this.str+=pre+k+'=>ARRAY[\n';
this.deep++;//同对象
this.show_obj(obj[k]);
pre = this.get_pre(this.deep);
this.deep--;//同对象
this.str+=pre+']\n';
}
}
return this.str;
},
alert_obj:function(obj)
{
alert(this.show_obj(obj))
}
}
my.alert_obj({a:{b:{c:{d:'hello world'}}}});
//-->
</script>
chrome 可以用 console.log
ie的话,
标签:
print_r,输出换行
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
桃源资源网 Design By www.nqtax.com
暂无“用js写了一个类似php的print_r输出换行功能”评论...