如何使用< script type =" text / template">来创建嵌入式HTML模板?使用jquery [英] How can I create an embeded HTML template using <script type="text/template"> using jquery

查看:90
本文介绍了如何使用< script type =" text / template">来创建嵌入式HTML模板?使用jquery的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建HTML模板,可以使用脚本标记调用表单jQuery,然后使用jQuery显示HTML。

I would like to create HTML templates that can be called form jQuery using script tags and then be able to show the HTML using jQuery.

<script type="text/template">
   <div id="new-post">
     <form>
       <label>Post Title</label>
       <input type="text" id="post-title"/>
       <input type="submit" value="Save"/>
     </form>
   </div>
</script>

我在stackoverflow.com上发现了其他帖子,但没有一个是我正在寻找的。任何意见,如何得到这个工作,将不胜感激。

I have found other post on stackoverflow.com, but none are what I'm looking for. Any advice on how to get this working would be appreciated.

推荐答案

您正在尝试应该工作正常。如果您有这样的内容:

What you're trying should work OK. If you have something like this:

<script type="text/html" id="post-template">
  <div id="new-post">
    <form>
      <label>Post Title</label>
      <input type="text" id="post-title"/>
      <input type="submit" value="Save"/>
    </form>
  </div>
</script>

您应该可以执行此操作来提取模板:

You should be able to do this to extract the template:

var template = $('#post-template').html();

更好的是,使用jQuery模板进行研究。请参阅此文档,以便全面了解它们的工作原理。

Better yet, look into using jQuery templates. See this documentation for a good overview of how they work.

这篇关于如何使用&lt; script type =&quot; text / template&quot;&gt;来创建嵌入式HTML模板?使用jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆