已提出(并回答)类似的问题,但没有关于如何修复它的答案/解决方案
我正在为我的 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">
最佳答案
好的,所以我找到了解决方案 from this thread .
问题是,当您尝试获取 JavaScript 字符串的 html 时,您可能会得到零宽度空格
。
Unicode 具有以下零宽度字符:
- U+200B 零宽度空格
- U+200C 零宽度非连接 Unicode 代码点
- U+200D 零宽度连接符 Unicode 代码点
- U+FEFF 零宽度不间断空格 Unicode 代码点
因此,为了解决我的问题,我使用正则表达式删除 unicode 字符:
var source = $("#guideListTemplate").html().replace(/[\u200B]/g, '');
关于jquery - 为什么我会收到这个奇怪的 “​” 换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28738650/