简体   繁体   English

小胡子模板与Javascript

[英]Mustache template with Javascript

I'm trying, without success, to add a javascript to a mustache template 我正在尝试将JavaScript添加到小胡子模板中,但没有成功

<script id="nodeTemplate" type="text/template">
  {{#myItem}}
    <div class="divclass {{#myItemDet}}">
    ...
    </div>


    <script type="text/javascript">
     alert('{{#myItemDet}}');
    </script>

  {{/myItem}}
</script>

Maybe, it is not possible with Mustache to create JS script... 也许,使用Mustache不可能创建JS脚本...

--------------------------- NOTE 1 ---------------------------注1
If a embeed <script type="text/javascript"> ... <script> inside a <script id="nodeTemplate" type="text/template">...</script> I suspect that mustache close the template at the first tag 如果在<script id="nodeTemplate" type="text/template">...</script>的嵌入的<script type="text/javascript"> ... <script> <script id="nodeTemplate" type="text/template">...</script>我怀疑是胡须关闭了模板在第一个标签

Is it true? 是真的吗

Riccardo 里卡多

--------------------------- NOTE 2 - - - - - - - - - - - - - - 笔记2
This is my solution: 这是我的解决方案:

1) fill with mustache several hidden divs with the class jshidden 1)用类jshidden的胡须填充几个隐藏的div

2) Search objs with class=jshidden and append div-content as javascript 2)使用class = jshidden搜索objs,并将div-content附加为javascript

The solution now work only on Chrome... Still working 该解决方案现在仅适用于Chrome。

http://jsfiddle.net/ric79/zWEUK/ http://jsfiddle.net/ric79/zWEUK/

You are using the block form for myItemDet , rather than the value form. 您正在将块形式用于myItemDet ,而不是值形式。

Try this 尝试这个

<script id="nodeTemplate" type="text/template">
  {{#myItem}}
    <div class="divclass {{myItemDet}}">
    ...
    </div>


    <script type="text/javascript">
     alert('{{myItemDet}}');
    </script>

  {{/myItem}}
</script>

Maybe try to obfuscate the ending </script> tag to not have the browser's parser take it for an inline script. 也许尝试混淆 </script>标记,以免浏览器的解析器将其用作嵌入式脚本。 Split it apart, just like advertisers do when writing to the document (eg document.write('<'+'/script>'); ). 将其分开,就像广告客户在写入文档时所做的一样(例如document.write('<'+'/script>'); )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM