开发者

How can I create an embeded HTML template using <script type="text/template"> using jquery

开发者 https://www.devze.com 2023-03-25 21:15 出处:网络
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.

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" va开发者_如何学Pythonlue="Save"/>
     </form>
   </div>
</script>

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

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


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

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

精彩评论

暂无评论...
验证码 换一张
取 消