Javascript多行字符串和Unexpected Token ILLEGAL [英] Javascript Multi-line string and Unexpected Token ILLEGAL

查看:102
本文介绍了Javascript多行字符串和Unexpected Token ILLEGAL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a multi-line string template in it's own separate js file for use in underscore js. However, no matter how I escape the line breaks I still get an:


未捕获的SyntaxError:意外的令牌ILLEGAL

Uncaught SyntaxError: Unexpected token ILLEGAL

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

On line 1 of the file when it's loaded into the browser.

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 -->';

任何想法?

推荐答案

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

Line terminators are not allowed in strings, you need to escape them:

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 -->';

为了便于查看行终止符的位置,可以打开文本中的可见空格编辑器你应该看到这样的东西:

To make it easier to see where you have line terminators, you can turn on visible whitespace in your text editor and you should see something like this:

这篇关于Javascript多行字符串和Unexpected Token ILLEGAL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆