<脚本>Underscore.js 模板中的标签?

发布于 2024-12-18 22:55:50 字数 416 浏览 2 评论 0原文

我在运行 Underscore 模板内的嵌套 JavaScript 时遇到问题。

<script type="text/template" id="myTemplate">
   <h3><%= heading %></h3>
   <div class="container"></div>

   //THIS DOES NOT WORK --->
   <script type="text/javascript" charset="utf-8">
     $(".container").html("Test")
   </script>
   //END

</script>

这是否可能,或者我该如何解决这个问题? 谢谢。

I'm having trouble getting nested JavaScript inside Underscore templates to run.

<script type="text/template" id="myTemplate">
   <h3><%= heading %></h3>
   <div class="container"></div>

   //THIS DOES NOT WORK --->
   <script type="text/javascript" charset="utf-8">
     $(".container").html("Test")
   </script>
   //END

</script>

Is this at all possible or how could I work around the issue?
Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

巨坚强 2024-12-25 22:55:51

我已经使用这种方法来模板化 html 页面。

<script type="text/template" id="mypage">
  <!DOCTYPE html>
  <html>
    <body>
      <script type="text/javascript" src="jquery.min.js"><%= "<"+"/script>" %>
    </body>
  </html>
</script>

I have used this method for templating an html page.

<script type="text/template" id="mypage">
  <!DOCTYPE html>
  <html>
    <body>
      <script type="text/javascript" src="jquery.min.js"><%= "<"+"/script>" %>
    </body>
  </html>
</script>
辞旧 2024-12-25 22:55:50
<script type="text/template" id="myTemplate">
   <h3><%= heading %></h3>
   <div class="container"></div>

   //THIS WORK :) --->

   <% print("<sc" + "ript type='text/javascript'>"); %>

     $(".container").html("Test")

    <% print("</sc"+"ript>"); %>

   //END

</script>

我必须这样做,因为我必须生成未知数量的函数。

<script type="text/template" id="myTemplate">
   <h3><%= heading %></h3>
   <div class="container"></div>

   //THIS WORK :) --->

   <% print("<sc" + "ript type='text/javascript'>"); %>

     $(".container").html("Test")

    <% print("</sc"+"ript>"); %>

   //END

</script>

I had to do so, because I had to generate an unknown number of functions.

挽梦忆笙歌 2024-12-25 22:55:50

Javascript 当然不属于模板。因此,它们不起作用才有意义。

如果需要执行与模板相关的脚本,请将它们放在一个函数中,并在渲染模板时调用它们。

Javascript certainly doesn't belong into a template. So it only makes sense that they do not work.

If you need to execute scripts related to the template, put them in a function and call them when you render the template.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文