2011-09-22 66 views
0

我在我的網站上使用jQuery模板,我想把它們全部放在一個單獨的文件中。它是如何做到的?把jquery模板放在外部文件中

下面是一個例子

 <script type="text/template" id="request-template">......</script> 

回答

2

如果你有一個大的應用程序,我建議你看看require.js 結合的text plugin

require.js允許在需要的時候,你動態加載腳本, 文本插件是理想的負載文本內容文件的方式與您在javascript模塊上使用它的方式相同。所以你可以把你的模板放在單獨的文件中,並將它們列爲你的javascript的依賴項,然後當javascript需要它們的時候它們會被加載。

編輯 簡單的解決辦法:把你的腳本塊在一個單獨的HTML文件,使用jQuery加載它們在

separatefile.html

<script type="text/template" id="user-template">user content here...</script> 
<script type="text/template" id="form-template">form content here...</script> 
<script type="text/template" id="mail-template">mail content here...</script> 

腳本:

$(function(){ 
    $('#templates').load("/separatefile.html", function(){ 
    // handle everything after you have your templates loaded... 
    }); 
}); 
+0

發揮得很好我的應用剛剛開始。任何簡單的方法來做到這一點本機? –

0

包括JavaScript文件像任何其他?

+1

一個問題是,您不會拿出

  • 11. 如何把控制器放在外部html文件中angularjs
  • 12. 把django模板標籤放在靜態文件中
  • 13. _underscore模板 - 從外部文件中加載模板
  • 14. [JQuery Mobile]在外部HTML文件中使用公共部分的Mutlipage模板
  • 15. 把外部文件放在eclipse裏相對
  • 16. 在模板中播放2.0框架外部模型
  • 17. 把數據放到外部html表中
  • 18. 把功能放到模板中
  • 19. 把按鈕放在masterdetail模板上xcode
  • 20. 如何將jQuery小鬍子應用到外部文件/模板
  • 21. Smarty的3來自外部的模板文件夾模板
  • 22. 在外部JavaScript文件中使用Jinja2模板引擎
  • 23. PHP在佈局模板中添加外部css和js文件
  • 24. 如何加載外部jQuery模板
  • 25. 使用node.js在外部文件中放置骨幹模型
  • 26. Dust.js外部模板
  • 27. 外部mustachr模板
  • 28. 如果把外部文件testthat測試
  • 29. 播放swf外部文件
  • 30. 在車把模板