关于格式化jason显示在页面上
我是小强
共 3052字,需浏览 7分钟
·
2024-04-11 07:30
json-viewer
下载 这个js 的插件 代码
https://github.com/abodelot/jquery.json-viewer
在页面上加载
如果是动态页面,记得另外写一个调用的js 方法
如下
页面加载3秒后才调用相应的数据
window.onload = function() {
var inputfromText;
setTimeout(function() {
var iframes = document.getElementsByTagName('iframe');
for (var i = 0; i < iframes.length; i++) {
var iframeDoc = iframes[i].contentDocument || iframes[i].contentWindow.document;
var iframeJquery = iframes[i].contentWindow.$;
var textareaElem = iframeDoc.getElementById('pr0_testassignments');
if (textareaElem) {
var newScript = document.createElement("script");
newScript.type = "text/javascript";
newScript.src = "WEB-CORE/extscripts/jquery/json-viewer/jquery.json-viewer.js";
iframeDoc.head.appendChild(newScript);
// Create new link element and add it to the iframe's head
var newLink = document.createElement("link");
newLink.rel = "stylesheet";
newLink.href = "WEB-CORE/extscripts/jquery/json-viewer/jquery.json-viewer.css";
iframeDoc.head.appendChild(newLink);
var specificTable = iframeJquery('#sdiedit');
specificTable.after($('<table width="" height="" border="0" cellspacing="0" style="background-color: white" cellpadding="0" id="table_json_view" currenttab="0"> <tr><td class="maintform_fieldtitle">Viewer</td><td class="maintform_fieldtitle" ><pre id="json-renderer"></pre></td></tr> </table>'));
try {
inputfromText = JSON.parse(textareaElem.value);
} catch (error) {
console.log('Invalid JSON', error);
}
break;
}
}
for (var i = 0; i < iframes.length; i++) {
var rendererElem = iframes[i].contentDocument.getElementById('json-renderer');
if (rendererElem) {
$(rendererElem).jsonViewer(inputfromText);
break;
}
}
}, 3000);
};
评论