coder

遍历原生table中的内容到json

2019-09-26  本文已影响0人  S拒绝拖延
    function fore() {
            var tabLen = document.getElementById("table");
            var jsonT = [];
            for (var i = 0; i < tabLen.rows.length; i++) {
                if (tabLen.rows[i].cells[2] != null) {
                    var name = tabLen.rows[i].cells[2].innerHTML.replace(/<[^>]+>/g, "").trim();
                }
                if (tabLen.rows[i].cells[3] != null) {
                    var comment = tabLen.rows[i].cells[3].innerHTML.replace(/<[^>]+>/g, "").trim();
                }
                if (i == 0) {
                } else {
                    var json = {};
                    if (name != null) {
                        json.name = name;
                    }
                    if (comment != null) {
                        json.comment = comment;
                    }
                    jsonT.push(json);
                }

            }
            console.table(jsonT);
        }
上一篇下一篇

猜你喜欢

热点阅读