已经提出(并回答了)类似的问题,但没有解决方法/解决方法
我在我的Phonegap项目中使用jQuery Mobile / Handlebars。到目前为止,一切似乎都很好。但突然间,我得到了这个奇怪的换行符:
"​ "
我使用以下代码制作列表:
// 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">
答案 0 :(得分:6)
好的,我找到了解决方案from this thread。
问题在于,当您尝试获取javascript字符串的html时,可能会获得zero width space
。
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?