基于javascript的JSON格式页面展示美化方法
{"name":"monkey","age":"24","height":164.0}
如果想让以上json字符串在页面上展示的比较易读,即变成下面的style:
{
"name":"monkey",
"age":"24",
"height":164.0cm
}
本文介绍的方法基于javascript,代码如下:
<html>
<head>/
//style中是css代码
<styletype="text/css">
body
{
white-space:pre;
font-family:monospace;
}
</style>
//script中是javascript代码
<script>
window.error_id_msgs=<%=error_id_msgs|raw%>;
functionmyFunction(){
document.body.innerHTML="";
document.body.appendChild(document.createTextNode(JSON.stringify(window.error_id_msgs,null,4)));
}
</script>
</head>
<bodyonload="myFunction()">//表示页面加载时调用myFunction()
</body>
</html>
其中window.error_id_msgs是所要转换的json对象,css和myFunction结合可实现页面展示转换
热门推荐
10 祝女儿简短祝福语大全
11 大学新年祝福语简短创意
12 元旦适合的祝福语简短
13 朋友出远门祝福语简短
14 初六简短的祝福语
15 祝男孩生日祝福语简短
16 同事调离的祝福语简短
17 拜年红包的祝福语简短
18 妈妈生日祝福语简短励志