为什么我会得到这个奇怪的“”换行符?

时间:2015-02-26 09:28:13

标签: jquery jquery-mobile handlebars.js

已经提出(并回答了)类似的问题,但没有解决方法/解决方法

我在我的Phonegap项目中使用jQuery Mobile / Handlebars。到目前为止,一切似乎都很好。但突然间,我得到了这个奇怪的换行符:

"​                               "

enter image description here

我使用以下代码制作列表:

    // HTML
    <ul id="guideListView" data-role="listview" ></ul>

    <script id="guideListTemplate" type="text/x-handlebars-template">​
        {{#if this}}
            {{#each this}}
            <li class="guide">
                <a href="{{guideUrl}}{{id}}" data-transition="slide" class="ui-nodisc-icon" >
                    <div class="name">{{name}}</div>
                    <div class="num-stores-container no-bold small">Stores: <span class="num-stores">{{storesCount}}</span></div>
                </a>
            </li>
            {{/each}}
        {{else}}
            <li class="ui-btn">Sorry, no guides for <span class="city"></span></li>
    {{/if}}
    </script>

    // JS
    var template = Handlebars.compile($("#guideListTemplate").html());
    $('#guideListView').append(template(guides));
    $('#guideListView').listview().listview('refresh');

有谁知道可能导致这种情况的原因?

更新
我已尝试使用("#guideListTemplate").html().trim()$('#guideListView').html(template(guides));,但这没有任何区别。这可能是jQuery Mobile的一大特色吗?

多一点调试,似乎问题可能在于:

<script id="guideListTemplate" type="text/x-handlebars-template">​

3 个答案:

答案 0 :(得分:6)

好的,我找到了解决方案from this thread

问题在于,当您尝试获取javascript字符串的html时,可能会获得zero width space

Unicode具有以下零宽度字符:

  • U + 200B零宽度空间
  • U + 200C零宽度非连接器Unicode代码点
  • U + 200D零宽度连接器Unicode代码点
  • U + FEFF零宽度不间断空格Unicode代码点

所以要解决我的问题,我使用正则表达式来删除unicode charecter:

var source = $("#guideListTemplate").html().replace(/[\u200B]/g, '');

答案 1 :(得分:5)

我遇到了同样的问题并找到了解决方案。问题源于我们将片段从www复制粘贴到我们的编辑器中,有时会导致像space这样的字符被复制为数字实体。编辑器解析实体,所以一切看起来都很普通。解决方案是找到一种方法来突出显示数字实体(如果它没有在编辑器中内置,查找扩展名)并删除它。 正则表达式/替换不是解决方案

答案 2 :(得分:1)

我的问题是Handlebar模板是用filencoding&#34; UTF-8与BOM&#34;一起保存的。但它需要保存为&#34; UTF-8,无BOM&#34;

如果您正在使用像Webstorm这样的Jetbrains IDE,只需右键单击该文件,然后单击&#34;删除BOM&#34;。

您可以在此处详细了解这两者之间的区别:What's different between UTF-8 and UTF-8 without BOM?