小提琴的工作示例,JQuery
0. 应用技巧!:JSON & localStorage
/* To store JSON in localStorage, you compress it as string */
localStorage["results"] = JSON.stringify(data); // or lS.restults
/* Whenever you want to work on it, need to uncompress the JSON */
var data = JSON.parse(localStorage["results"]);
1. 创建元函数:getJsonVal()
//META.Fn: pullVal
function getJsonVal(json, itemId) {
for (var i in json) {
if (json[i].myKey == itemId) {
return json[i];
}
}
}
2. 在注入CSS类之前检查JSON值的函数
//META.Fn: loadSwitch [check localStorage & set CSS
function loadSwitchCSS($set, i) {
setTimeout(function () {
var myID = $set.eq(i).attr('id'); // a. Get key [my case: from the html ID]
alert("look at:"+ myID);
var val = getJsonVal(data, myID).status;
// alert(data);
if (val == 1) { // c. CSS remove-add: so if...else... CSS
$set.eq(i).removeClass().addClass('status1');
} else {
$set.eq(i).removeClass().addClass('status0')
}
if (i < $set.length - 1) {
loadSwitchCSS($set, i + 1);
}
}, 100);
}
3. 加载时触发函数:
loadSwitchCSS($('p'), 0);