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
Configuration file that needs to be loaded when t...
background All of the company's servers are p...
Problem Description Recently, when I was building...
Some special characters and icons used in the pro...
This article shares the specific code for impleme...
Table of contents Steps to create TCP in Linux Se...
1. Some tips on classes declared with class in re...
Some time ago, during development, I encountered ...
About derived tables When the main query contains...
The following analysis is about product design pr...
Seurat is a heavyweight R package for single-cell...
nginx is our most commonly used server, often use...
The outermost boxF rotates 120 degrees, the secon...
Inside the style tag of the vue component, there ...
Project scenario: 1. Upload file restrictions Fun...