我需要为一个类使用本地存储,并且我正在使用 JSON 的 stringify 函数来执行此操作。我正在尝试解析它,但由于某种原因它不起作用。当我使用我的 if 语句时, ct2 和 cn2 实际上并没有工作。这里首先是html。
<div data-role="page" id="page1">
<div data-role="header">
<h1>Graphic Design</h1>
</div>
<div data-role="content" id="classList">
<div id="classes" data-role="listview"></div>
</div>
<div data-role="footer" data-position="fixed" data-fullscreen="true">
<h4>Department of Art</h4>
</div>
</div>
这是名为“json.js”的 JSON javascript 文件
var cn = ["Art 150 Drawing 1", "Art 170 Basic Design 1", "Art 171 Basic Design 2", "Art 274 Typography 1", "Art 282 Computer Art 1", "Art 235 Ancient/Medevil Art", "Art 236 Renaissance/Mid-19th Century", "Art 237 Impressionism/Post Modernism", "Art 324 History of Photography", "Art 225 Photo 1 or Art 226 Digital Photo", "Art 351 Illustration 1", "Art 374 History of Graphic Design", "Art 370 Layout & Design 1", "Art 371 Layout & Design 2", "Art 372 Design & Production 1", "Art 451 Illustration 2", "Art 470 Advertising Graphic Design", "Art 471 Corporate Graphic Design", "Art 472 Typography 2", "Art 490 Internship", "Art 495 Portfolio Class"]
var ct = ["n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n", "n"]
localStorage.setItem("classTaken", JSON.stringify(ct));
localStorage.setItem("className", JSON.stringify(cn));
这是我的代码“retrievesData”并尝试将解析的信息以列表视图格式输出到“#classList”div。
$(document).ready(function () {
$.getJSON("json.js", function (data) {
var retrievedData = localStorage.getItem("className");
var cn2 = JSON.parse(retrievedData);
var retrievedData = localStorage.getItem("classTaken");
var ct2 = JSON.parse(retrievedData);
for (var i = 0; i < ct.length; i++) {
var info = '<li>' + cn2[i] + '</li>'
if (ct2[i] == "y") {
$('#classes').append(info);
} else {
$('#notTaken').append(info);
}
}
});
});