json字符串在页面上格式化展示;

代码仅限于将json字符串转格式化;

  • 将json字符串格式化并展示在页面上;
  • 并且将文本框自动适应高低;

 效果图就不上传了自行体验

上代码:

/*必须引入的js*/
/*jquery也是需要的*/
<script type="text/javascript" src="http://tools.jb51.net/static/jsformat/jsbeautify.js"></script>
<script type="text/javascript">
    function do_js_beautify(returnDate) {
        if (returnDate.length == 0)
            return;
        var tabsize = 4;
        tabchar = ' ';
        if (tabsize == 1)
            tabchar = '	';
        var fjs = js_beautify(returnDate, tabsize, tabchar);
        $("#returnsInfoSpanId").html(fjs);
        //调用文本域自适应
        autoTextAreaHeight($("#returnsInfoSpanId")[0])
    }

    //文本域自适应
    function autoTextAreaHeight(o) {
        o.style.height = 0;
        o.style.height = o.scrollTop + o.scrollHeight + 30 + "px";
    }
</script>

<!--HTML 页面部分-->

<span>
     <textarea id="returnsInfoSpanId" style=" 440px;padding:0px;" readonly>
     </textarea>
</span>

json串可以是后台返回的 也可以是自己复制进去的都一样 具体怎么使用可灵活修改;

  很简单,告辞;

原文地址:https://www.cnblogs.com/hb-liang/p/12082654.html