【问题标题】:Embedding a script with a jsrender template使用 jsrender 模板嵌入脚本
【发布时间】:2012-07-27 13:58:19
【问题描述】:

我想使用我的 jsrender 模板中的一个变量来生成 javascript 的 sn-p,但我不知道如何在我的 <script id="data-item-tpl" type="text/x-jsrender"> 中放置一个 <script type="text/javascript">

希望以下内容相当清楚。下面的代码产生一个Uncaught SyntaxError。如果我从嵌入脚本中删除标签,那么它只是作为文本打印到页面上。

在下面的代码中,我试图生成一个 id 为“chartId”的 div,然后是一个脚本,该脚本将通过 MyChart 对象用内容填充该 div。

<script id="data-item-tpl" type="text/x-jsrender">
    <div class="item">
        <div class="graphs">
            <ul class="thumbnails">
                {{for graphs}}
                <li><div class="thumbnail"><img src="{{html:url}}" width="190" height="120" /><p>{{html:graphTitle}}<br />{{html:value}}</p></div></li>
                {{/for}}
                <!-- Here we insert both the placeholder for, and the javascript to populate charts -->
                <li><div id="{{html:chartId}}" style="width:120;height:190px;"></div></li>
                <script type="text/javascript">
                    var chartObj = new MyChart("{{html:chartId}}");
                    chartObj.render();
                </script>
            </ul>                           
        </div>
        ...
</script>

【问题讨论】:

    标签: javascript jsrender


    【解决方案1】:

    我只是玩了一下,就让它像这样工作:

    {{:"<scr" + "ipt type=&quot;text/javascript&quot;>"}}
        var chartObj = new MyChart("{{html:chartId}}");
        chartObj.render();
    {{:"</scr" + "ipt>"}}
    

    也许不是最优雅的解决方案,但是当我在其中输入 alert 时,它触发了。

    【讨论】:

    • 是的!这是每个编写 Web 代码的人都必须经常忍受的代码质量。网络技术很烂。时期。谢谢!
    • 我在监视窗口中收到“太多非法字符”错误。我无法使用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多