HTML代码转换为JavaScript字符串

<script type="text/javascript">
    function javascript() {
        var isArraySel = $("#arrays-sel").prop("checked");
        var htmlArr = $("#content").val().replace(/\\/g, "\\\\").replace(/\\/g, "\\/").replace(/\'/g, "\\\'").replace(/\"/g, "\\\"").split('\n');
        var len = htmlArr.length;
        var outArr = [];
        if (isArraySel) {
            outArr.push("[");
            jQuery.each(htmlArr, function (index, value) {
                if (value !== "") {
                    if (index === len - 1) {
                        outArr.push("\"" + value + "\"");
                    } else {
                        outArr.push("\"" + value + "\",\n");
                    }
                }

            });
            outArr.push("].join(\"\");");
        } else {
            jQuery.each(htmlArr, function (index, value) {
                if (value !== "") {
                    if (index === len - 1) {
                        outArr.push("\"" + value + "\";");
                    } else {
                        outArr.push("\"" + value + "\"+\n");
                    }
                }
            });
        }
        $("#result").val(outArr.join(""));
    }

 

posted @ 2017-03-27 20:24  catgatp  阅读(6687)  评论(0编辑  收藏  举报