基于localStorage内容的JavaScript和条件格式

时间:2017-09-04 02:47:25

标签: javascript html5 local-storage

我正在写一个GreaseMonkey脚本,为我的雇主的内部求职网站做一些急需的改进。

此处包含的循环遍历作业搜索返回的表中的所有行。我在每行的左侧添加了图标,以跟踪通信,接受和拒绝。数据作为转换为字符串的JSON对象记录在浏览器localStorage中。示例localStorage条目如下所示:

job_12345 = '{"accept":"9/3/2017, 6:50 PM","reject":"9/3/2017, 6:50 PM"}'

在下面的函数中,lstore是一个localStorage键+值,如上例所示。循环工作正常,直到我尝试添加一些条件格式,显示"禁用"对于localStorage中不存在的工作行为,图标(实际上只有50%的不透明度)。

我的循环底部附近的三行IF语句是我无法弄清楚的。它在数组中第一次工作,然后数组断开,函数结束。



function checkRows() {
    if (document.getElementsByClassName("jstat") != null) {
        clearInterval(startIt2);
        var jobs2 = document.getElementById('results').getElementsByClassName('jrow');

        for (var j = 0; j < jobs2.length; j++) {
            // conditional formatting by status
            var job_status = jobs2[j].getElementsByClassName("jstat")[0].innerText;
            job_status = job_status.toLowerCase();
            job_status = job_status.replace(/ /g, "_");

            // apply conditional class to each row
            jobs2[j].classList.add(job_status);

            // get job id for shortcut buttons
            var jid = jobs2[j].getElementsByTagName("input")[0].value;

            // get the table cell where the buttons go
            var job_box = jobs2[j].getElementsByClassName("col-chk")[0];
            job_box.classList.add("icon");

            // read localStorage for selected job
            var lstore = JSON.parse(localStorage.getItem("job_" + jid));

            // array for adding buttons
            var arr_acts = ["mail", "accept", "reject"];

            for (var a = 0; a < arr_acts.length; a++) {
                // get the action from the array
                var action = arr_acts[a];
                // create a new span to hold the icon
                var span = document.createElement("span");
                span.setAttribute("data-jobid", jid);
                span.setAttribute("data-action", action);
                span.id = (action + jid);
                span.classList.add(action);
                span.onclick = toggleLocal;
                //span.classList.add("disabled");
                // set the icon for each action
                var icon;
                switch (action) {
                    case "mail":
                        icon = "&#x01f4e7;";
                        break;
                    case "accept":
                        icon = "&#x1F44D;";
                        break;
                    case "reject":
                        icon = "&#x1F44E;";
                        break;
                }
                // if the action doesn't exist in localStorage, set the class to "disabled"
                console.log(lstore);
                // *** this is where the loop breaks *** //
                if (lstore.hasOwnProperty(action)) {
                    span.classList.add("disabled");
                }
                span.innerHTML = icon;
                // render the icon
                job_box.appendChild(span);
            }
        }
    }
}
&#13;
&#13;
&#13;

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我相信我已经想到了这一点 - 如果该作业ID没有任何内容,我调用localStorage(lstore)的变量为null,这似乎已经破坏了我的IF语句。这很有效:

getcwd()