9

我在它自己的单独 js 文件中有一个多行字符串模板,用于下划线 js。但是,无论我如何逃脱换行符,我仍然得到:

Uncaught SyntaxError: Unexpected token ILLEGAL

在文件加载到浏览器时的第 1 行。

App.Templates['template1'] = '\

<div data-role="page" data-theme="c" id="" data-title="">\

    <div data-role="content" class="subnav">\

        <table id="day-table" cellpadding="0" cellspacing="0" border="0">\
            <thead class="ui-body-a">\
                <tr>\
                    <th>T</th>\
                    <th>J</th>\
                    <th>H</th>\
                    <th>C</th>\
                </tr>\
            </thead>\
            <tbody>\
            </tbody>\
            <tfoot>\
                <tr>\
                    <td class="total-label" colspan="2">Total:</td>\
                    <td class="total"></td>\
                </tr>\
                <tr>\
                    <td class="btn-row">\
                        <a href="#r" data-role="button" id="add-btn" data-rel="dialog" data-mini="true" data-inline="true" data-icon="add">Add Rows</a>\
                        <a href="#" data-role="button" id="save-btn" data-rel="dialog" data-mini="true" data-inline="true" data-theme="b" data-icon="check">Save</a>\
                    </td>\
                </tr>\
            </tfoot>\
        </table>\

    </div><!--/content-->\

</div><!-- /page -->';

有任何想法吗?

4

2 回答 2

11

字符串中不允许使用行终止符,您需要对它们进行转义:

App.Templates['template1'] = '\
\
<div data-role="page" data-theme="c" id="" data-title="">\
\
    <div data-role="content" class="subnav">\
\
        <table id="day-table" cellpadding="0" cellspacing="0" border="0">\
            <thead class="ui-body-a">\
                <tr>\
                    <th>T</th>\
                    <th>J</th>\
                    <th>H</th>\
                    <th>C</th>\
                </tr>\
            </thead>\
            <tbody>\
            </tbody>\
            <tfoot>\
                <tr>\
                    <td class="total-label" colspan="2">Total:</td>\
                    <td class="total"></td>\
                </tr>\
                <tr>\
                    <td class="btn-row">\
                        <a href="#r" data-role="button" id="add-btn" data-rel="dialog" data-mini="true" data-inline="true" data-icon="add">Add Rows</a>\
                        <a href="#" data-role="button" id="save-btn" data-rel="dialog" data-mini="true" data-inline="true" data-theme="b" data-icon="check">Save</a>\
                    </td>\
                </tr>\
            </tfoot>\
        </table>\
\
    </div><!--/content-->\
\
</div><!-- /page -->';

为了更容易查看行终止符的位置,您可以在文本编辑器中打开可见空白,您应该会看到如下内容:

在此处输入图像描述

于 2012-08-24T19:10:38.187 回答
2

我想到了。空行也需要转义。

App.Templates['template1'] = '\
\
<div data-role="page" data-theme="c" id="" data-title="">\
\
    <div data-role="content" class="subnav">\
\
        <table id="day-table" cellpadding="0" cellspacing="0" border="0">\
            <thead class="ui-body-a">\
                <tr>\
                    <th>T</th>\
                    <th>J</th>\
                    <th>H</th>\
                    <th>C</th>\
                </tr>\
            </thead>\
            <tbody>\
            </tbody>\
            <tfoot>\
                <tr>\
                    <td class="total-label" colspan="2">Total:</td>\
                    <td class="total"></td>\
                </tr>\
                <tr>\
                    <td class="btn-row">\
                        <a href="#r" data-role="button" id="add-btn" data-rel="dialog" data-mini="true" data-inline="true" data-icon="add">Add Rows</a>\
                        <a href="#" data-role="button" id="save-btn" data-rel="dialog" data-mini="true" data-inline="true" data-theme="b" data-icon="check">Save</a>\
                    </td>\
                </tr>\
            </tfoot>\
        </table>\
\
    </div><!--/content-->\
\
</div><!-- /page -->';
于 2012-08-24T19:11:55.080 回答