DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> 基於javascript的JSON格式頁面展示美化方法
基於javascript的JSON格式頁面展示美化方法
編輯:關於JavaScript     
{"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結合可實現頁面展示轉換

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved