首頁  >  文章  >  web前端  >  基於javascript的JSON格式頁面展示美化方法_javascript技巧

基於javascript的JSON格式頁面展示美化方法_javascript技巧

WBOY
WBOY原創
2016-05-16 16:42:411293瀏覽
{"name": "monkey","age": "24","height": 164.0}

如果想讓以上json字串在頁面上展示的比較易讀,即變成下面的style:

{ 
"name": "monkey", 
"age": "24", 
"height": 164.0cm 
}

本文介紹的方法是基於javascript ,程式碼如下:

<html> 
<head>/ 
//style中是css代码 
<style type="text/css"> 
body 
{ 
white-space: pre; 
font-family: monospace; 
} 
</style>   
//script中是javascript代码 
<script> 
window.error_id_msgs = <%= error_id_msgs | raw %>; 
function myFunction() { 
document.body.innerHTML = ""; 
document.body.appendChild(document.createTextNode(JSON.stringify(window.error_id_msgs, null, 4))); 
}      
</script> 
</head> 
<body onload="myFunction()"> //表示页面加载时调用myFunction() 
</body> 
</html>

其中window.error_id_msgs是所要轉換的json對象,css和myFunction結合可實現頁面展示轉換

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn