jquery - 为什么我会收到这个奇怪的 “​” 换行符?

标签 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">​

最佳答案

好的,所以我找到了解决方案 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 - 为什么我会收到这个奇怪的 “&#8203;” 换行符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28738650/

相关文章:

Jquery slide toggle 让我的文本区域跳下来

css - jQuery Mobile 布局自定义

html - jQuery Mobile 文本框的宽度

javascript - 使用 Jquery Mobile 时,HTML A HREF 不断添加 # 和基本 url

javascript - 使用handlebars.js 登录状态相关菜单

javascript - 使用 jQuery,IE<9 无法将元素束附加到文本节点。这是 jQuery 错误吗?

javascript - 阻止结果,通过 AJAX 显示,重新加载浏览器后

javascript - JS 全局变量传递为未定义 - jquery mobile

javascript - Handlebars 困惑我不明白

ember.js - 在 Ember.js 中生成一个循环计数器 #each