如何使用jquery使用创建嵌入式HTML模板

时间:2021-07-22 21:39:54

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.

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

<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>

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

我在*.com上找到了其他帖子,但没有一个是我正在寻找的。任何有关如何使这项工作的建议将不胜感激。

2 个解决方案

#1


6  

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();

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

更好的是,考虑使用jQuery模板。请参阅此文档以获取有关其工作原理的详细概述。

#2


0  

Check out the jQuery load method as well as some of the other Ajax calls:

查看jQuery加载方法以及一些其他Ajax调用:

$('#result').load('ajax/test.html');

#1


6  

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();

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

更好的是,考虑使用jQuery模板。请参阅此文档以获取有关其工作原理的详细概述。

#2


0  

Check out the jQuery load method as well as some of the other Ajax calls:

查看jQuery加载方法以及一些其他Ajax调用:

$('#result').load('ajax/test.html');