繁体   English   中英

通过JavaScript在jQuery Mobile中打印Json文件

[英]Printing Json file in jquery mobile by javascript

基本上,我已经成功用html中的js函数打印了一个json文件。 从那以后,我切换到了jquery mobile,并尝试修改我的函数以使其与jquery mobile语法匹配。 我已经手动添加了应该打印出的功能,它可以正确显示,因此该功能存在问题。 我试图在ui-grid-d中进行打印,并通过每个块循环遍历数组,以保持正确的语法

JS功能:

<script type="text/javascript">
var grid= new Array();
grid[0]= "<div class='ui-block-a'>";
grid[1]= "<div class='ui-block-b'>";
grid[2]= "<div class='ui-block-c'>";
grid[3]= "<div class='ui-block-d'>";
var j=0;


$(function (){

  var imp = "Json/contents.json"

  $.getJSON(imp, function(data) {
    var toc="";
    var pos=grid[j];
    $.each(data.tcontent, function(index, item) {
      toc += pos + item.url + '<div class="grid">' + "<p class='gridtext'>" + item.Chapter + ":" + item.Name  + "</p>" +  "</div>" + "</a>" + "</div>"
    });

 //incriment j
 j++;

 //if j is out of bounds return j to 0
 if(j==4){
  j=0;
}
$(toc).appendTo(".ui-grid-d");
 //alert(toc)
 });
});
</script> 

Json File(忘记background属性):

{
 "tcontent": [{
"Chapter": "1",
"Name": "General Principles of Antibiotic Perscribing",
"url":"<a href='general_principles.html'>",
"Background":"yellow"
}, {
"Chapter": "2",
"Name": "Note on meticillin resistant SA",
"url":"<a>",
"Background":"background-color:red"
}, {
"Chapter": "3",
"Name": "Empirical Therapy Guidelines:",
"url": "<a href='empirical.html'>",
"Background":"background-color:red"
}, {
"Chapter": "4",
"Name": "Treatment of Malaria",
"url":"<a>",
"Background":"background-color:red"
}, {
"Chapter": "5",
"Name": "Antibiotic Prophylaxis:",
"url":"<a>",
"Background":"background-color:red"
}, {
"Chapter": "6",
"Name": "Aminoglycoside and Glycopeptide dosing and monitoring:",
"url":"<a>",
"Background":"background-color:red"
}, {
"Chapter": "7",
"Name": "Splenectomy: vaccination and antibiotic prophylaxis",
"url":"<a>",
"Background":"background-color:red"
}, {
"Chapter": "8",
"Name": "Restricted Antimicrobials",
"url":"<a>",
"Background":"background-color:red"
}, {
"Chapter": "9",
"Name": "Topical Antibiotics",
"url":"<a>",
"Background":"background-color:red"
}, {
"Chapter": "10",
"Name": "Antimicrobials and Renal Failure",
"url":"<a>",
"Background":"background-color:red"
}, {
"Chapter": "11",
"Name": "Antimicrobials and Hepatic Disease",
"url":"<a>",
"Background":"background-color:red"
}, {
"Chapter": "12",
"Name": "Administration of IV Antimicrobials",
"url":"<a>",
"Background":"background-color:red"
}, {
"Chapter": "13",
"Name": "Penicillin allergy & other beta-lactam containing antibiotics",
"url":"<a>",
"Background":"background-color:red"
}]
}

相关HTML:

     <body>
          <div data-role="page" data-theme="a" id="Page1">

            <div data-role="header" data-theme="c">
              <a data-rel="back" data-role="button" class="ui-btn-left" data-transition="flip" data-icon="back"> Back </a>
                <a href="info.html" data-role="button" class="ui-btn-right" data-transition="flip" data-icon="info"> Info </a>
                <h2>Main Menu</h2>
            </div>

            <div data-role="content" data-theme="c" id="Page1_Content">

            <div class="ui-grid-d">



            </div>

            <div data-role="footer" data-theme="c">
                <h2>(c) Darragh O'Connor </h2>
            </div>


        </div>



</body>

如果有人能看到问题,我将不胜感激。 我已经动脑了几个小时。 谢谢

这是一个演示

您需要在$.each()递增j ,并且如果j> 3,也将其设置为0。您在文本中还缺少<a>标记或多余</a> 您的变量pos也永远不会更改,因为它位于each()循环之外。 对于4列,网格类应为ui-grid-c

$(document).on("pageinit", "#Page1", function(){

    var toc= '';
    $.each(json.tcontent, function(index, item) {
        if (j > 3) { j = 0; }
        toc += grid[j] + item.url + '<div class="grid">' + "<a><p class='gridtext'>" + item.Chapter + ":" + item.Name  + "</p>" +  "</div>" + "</a>" + "</div>"
        j++;
    });

    $(toc).appendTo(".ui-grid-c");

});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM