使用JavaScript加载HTML模板 [英] Load HTML template with JavaScript
问题描述
我有templates.html,其中包含一个集合我想加载到JavaScript并使用的HTML代码片段。记住templates.html不是加载的DOM文档,访问模板/代码片段的好方法是什么?
我正在考虑使用 document.open
来创建一个DOM来访问,但我认为这在某些浏览器上有问题。
<您可以将html加载到隐藏的div中,然后您将拥有一个DOM访问
将html加载到DIV的最简单方法是使用jquery load: http://api.jquery.com/load/
I am struggling to find a clean solution to my problem and was wondering if anyone could offer some tips.
I have "templates.html" which contains a collection of HTML snippets which I want to load into JavaScript and use. What is a good way to access the templates/snippets bearing in mind that templates.html is not a loaded DOM document?
I was thinking about using document.open
to create a DOM to access but I think this has issues on certain browsers.
You can load the html into a hidden div and then you will have a DOM access the easiest way to load the html to a DIV is using jquery load: http://api.jquery.com/load/
这篇关于使用JavaScript加载HTML模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!