[英]Printing Json file in jquery mobile by javascript
Basically I've been successfully printing a json file with a js function in html. 基本上,我已经成功用html中的js函数打印了一个json文件。 I have since switched over to jquery mobile and tried to modify my function to match with the jquery mobile syntax. 从那以后,我切换到了jquery mobile,并尝试修改我的函数以使其与jquery mobile语法匹配。 I have manually added what the function should be printing out and it displays correctly so there is a problem with the function. 我已经手动添加了应该打印出的功能,它可以正确显示,因此该功能存在问题。 I am trying to print in ui-grid-d and have an array looping through each of the blocks in order to keep correct syntax 我试图在ui-grid-d中进行打印,并通过每个块循环遍历数组,以保持正确的语法
JS Function: 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 (forget about the background attribute): 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"
}]
}
Relevant HTML: 相关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>
If anyone could see the problem I'd much appreciate it. 如果有人能看到问题,我将不胜感激。 I've been racking my brains for hours. 我已经动脑了几个小时。 Thanks 谢谢
You need to increment j
within the $.each()
and also set it to 0 if j > 3. You also had a missing <a>
tag or and extra </a>
within the text. 您需要在$.each()
递增j
,并且如果j> 3,也将其设置为0。您在文本中还缺少<a>
标记或多余</a>
。 Your variable pos would also never change because it is outside the each() loop. 您的变量pos也永远不会更改,因为它位于each()循环之外。 For 4 columns, the grid class should be ui-grid-c
. 对于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.