使用src属性访问脚本标记的内容(包括模板)

时间:2012-11-05 17:40:21

标签: javascript jquery handlebars.js

  

可能重复:
  How to outsource a template js to a different file when using Handlebars.js

我正在使用手柄。他们建议在文档正文中包含模板,如下所示:

<script id="entry-template" type="text/x-handlebars-template">
     template content
</script>

我想通过src属性使用模板内容,并将我的模板(质量)存储在单独的文件中。

<script src="/path/to/my.template" id="entry-template" type="text/x-handlebars-template"></script>

问题是如何访问它的co内容?

$('script#entry-template').html() //returns ""
$('script#entry-template').text() //returns ""

1 个答案:

答案 0 :(得分:6)

您需要获取src属性的值,然后使用XMLHttpRequest请求URI ...此时您最好不要使用<script type="not-js">

浏览器不会自动下载未知的脚本类型,也不会使脚本以编程方式访问JS。您可以访问内联脚本,因为它们是DOM的一部分,而外部脚本则不是。

相关问题