我在我的網站上使用jQuery模板,我想把它們全部放在一個單獨的文件中。它是如何做到的?把jquery模板放在外部文件中
下面是一個例子
<script type="text/template" id="request-template">......</script>
我在我的網站上使用jQuery模板,我想把它們全部放在一個單獨的文件中。它是如何做到的?把jquery模板放在外部文件中
下面是一個例子
<script type="text/template" id="request-template">......</script>
如果你有一個大的應用程序,我建議你看看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...
});
});
包括JavaScript文件像任何其他?
一個問題是,您不會拿出
發揮得很好我的應用剛剛開始。任何簡單的方法來做到這一點本機? –