簡體   English   中英

通過ajax加載下划線模板會丟失html,需要轉義“ <”,“>”

[英]Loading underscore template via ajax loses html, need to escape “<”, “>”

我正在使用ajaxify.js( https://github.com/browserstate/ajaxify/blob/master/ajaxify-html5.js )加載頁面內容。 默認情況下,此腳本從頁面異步加載腳本,如下所示:

$scripts.each(function(){
    var $script = $(this), 
        scriptText = $script.text(),
        scriptType = $script.attr('type'),
        scriptId = $script.attr('id');

    scriptNode = document.createElement('script');

    if(scriptType) {
        $(scriptNode).attr('type', scriptType);
    }

    if(scriptId) {
        $(scriptNode).attr('id', scriptId);
    }

    scriptNode.appendChild(document.createTextNode(scriptText));
    contentNode.appendChild(scriptNode);
});

問題是在我的下划線模板中,使用$script.text()忽略模板的HTML內容。

另一方面,使用$script.html()從模板代碼中轉義<>字符(下划線的<%等...),並且輸出最終看起來像:

&lt;% _.each(things,function(thing,key,list){ %&gt;

我如何完整保留腳本的內部html / text,以使其在AJAX加載下正常運行? 謝謝!

完整的模板腳本為:

    <script type="text/html" id='furniture-template'>
        <div class="accordion collapse">
            <div class="accordion-group">
                <% _.each(things,function(thing,key,list){ %>

                <div class="accordion-heading">
                    <a class="no-ajaxy accordion-toggle ic-minus block collapsed" data-toggle="collapse" href="#things-<%= thing.slug %>">
                        <%= thing.title %>
                    </a>
                </div> <!-- header -->

                <div id="things-<%= thing.slug %>" class="accordion-body collapse">
                    <div class="accordion-inner">
                        <% for(var item in thing.items) { %>
                        <div class="item">
                            <% if( thing.items[item].images == true ) { %>
                                <a class="no-ajaxy" data-target="<%= thing.items[item].slug %>-gal" class="img-link ic-cam fl" title="View an example"></a>
                            <% } %>

                            <a 
                                class="item-add ic-plus" 
                                data-title="<%= thing.items[item].title %>" 
                                data-slug="<%= thing.items[item].slug %>"
                                data-img="<%= thing.items[item].images %>"
                                data-shorthand="<%= thing.items[item].shorthand %>"
                                data-price="<%= thing.items[item].price %>"
                            >
                                <%= thing.items[item].title %>
                            </a>
                        </div>
                        <% } %>
                    </div> <!-- inner -->
                </div> <!-- accordion-body -->

            <% }); %>
            </div>
        </div>
    </script>

奇怪的情況。 簡單的解決方案是使用.html()並轉義轉義的下划線序列,這可能就是您想要的(即,您可能不想使所有內容轉義):

$script.html().replace(/&lt;%/gi, '<%').replace(/%&gt;/gi, '%>')

您沒有嘗試過這個:

contentNode.innerHTML = scriptnode;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM