JSON data is displayed and formatted on the HTML page 1. Display effect diagram Description:
2. Source code display <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <style> pre {outline: 1px solid #ccc; padding: 5px; margin: 5px; } .string { color: green; } .number { color: darkorange; } .boolean { color: blue; } .null { color: magenta; } .key { color: red; } .showinfo{ position: absolute; background-color: #eef1f8; width: 200px; padding: 5px; border-radius: 4px; border: 1px solid #ccc; display: none; } .showinfo pre{ padding: 5px; border: 1px solid #ccc; margin:0; } table,th,td{ border:1px solid blue; } </style> <script src="./js/jquery-1.8.3.min.js"></script> <script type="text/javascript"> $(document).ready(function(){ $(".show-rough").mouseover(function(){ var left = $(this).offset().left + $(this).width() +20; //Calculate div display position var top = $(this).offset().top + 20; var _jsonDate = $.parseJSON($(this).text()); var showJson = syntaxHighlight(_jsonDate); $("#show-info").css({"left":left,"top":top}).show(); $("#show-pre").html(showJson); }); $(".show-rough").mouseout(function(){ $("#show-info").hide().html(); $("#show-pre").html(); }) }); //Process json data and use regular expressions to filter out parameters of different types function syntaxHighlight(json) { if (typeof json != 'string') { json = JSON.stringify(json, undefined, 2); } json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>'); return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function(match) { var cls = 'number'; if (/^"/.test(match)) { if (/:$/.test(match)) { cls = 'key'; } else { cls = 'string'; } } else if (/true|false/.test(match)) { cls = 'boolean'; } else if (/null/.test(match)) { cls = 'null'; } return '<span class="' + cls + '">' + match + '</span>'; }); }; </script> </head> <body> <table> <thead> <tr> <th>Name</th> <th>json data</th> </tr> </thead> <tbody> <tr> <td>小三</td> <td class="show-rough">{ "name": "小三", "address":"No. 23 Beijing Road","age":16, "email": "[email protected]","Object":[{"Position":"Manager"}],"del":[] }</td> </tr> <tr> <td>小四</td> <td class="show-rough">{ "name": "Xiao Si", "address":"No. 01 Shanghai Road","age":27, "email": "[email protected]","Object":[],"del":[] }</td> </tr> </tbody> </table> <div id="show-info" class="showinfo"> <pre id="show-pre"> </pre> </div> </body> </html> 3. Source code upload Source code download address This is the end of this article about how to display JSON data and format it on HTML pages. For more relevant content about how to display JSON and format it on HTML pages, please search for previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope you will support 123WORDPRESS.COM in the future! |
<<: Node script realizes automatic sign-in and lottery function
>>: A brief discussion on browser compatibility issues in JavaScript
The code looks like this: .process{ border:1px so...
Install Docker on CentOS 8 Official documentation...
Previously, the images we used were all pulled fr...
Table of contents 8. CSS3 click button circular p...
Method 1: Use the SET PASSWORD command MySQL -u r...
Docker installs MySQL version 8.0.20 for your ref...
Preface: Docker is an open source application con...
Overview Nginx load balancing provides upstream s...
touch Command It has two functions: one is to upd...
In some cases, the data in data needs to be reuse...
Table of contents Overview Environment Preparatio...
This article mainly summarizes various problems o...
Today we will implement a fragmented image loadin...
Download the compressed version of MySQL-5.7.11-w...
Preface To delete a table, the command that comes...