使用JavaScript加载HTML模板 [英] Load HTML template with JavaScript

查看:135
本文介绍了使用JavaScript加载HTML模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有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屋!

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