jQuery .tmpl() 用法示例介紹
動(dòng)態(tài)請(qǐng)求數(shù)據(jù)來(lái)更新頁(yè)面是現(xiàn)在非常常用的方法,比如博客評(píng)論的分頁(yè)動(dòng)態(tài)加載,微博的滾動(dòng)加載和定時(shí)請(qǐng)求加載等。
這些情況下,動(dòng)態(tài)請(qǐng)求返回的數(shù)據(jù)一般不是已拼好的 HTML 就是 JSON 或 XML,總之不在瀏覽器端拼數(shù)據(jù)就在服務(wù)器端拼數(shù)據(jù)。不過(guò),從傳輸量方面來(lái)看,返回 HTML 不劃算,而在 web 傳輸方面,現(xiàn)在更多的是使用 JSON 而不是 XML。
瀏覽器端根據(jù) JSON 生成 HTML 有個(gè)很苦惱的地方就是,結(jié)構(gòu)不復(fù)雜的時(shí)候還好,結(jié)構(gòu)一復(fù)雜,就想死了,需要很小心很小心地寫出幾乎無(wú)法維護(hù)的 JavaScript 代碼。
如同為解決 PHP 拼數(shù)據(jù)這方面的問(wèn)題而有了 Smarty 這些模版,JavaScript 也可以利用模版來(lái)解決這些問(wèn)題,比如基于 jQuery 的 jquery.tmpl,現(xiàn)在已經(jīng)被接受為官方的模版插件了。詳細(xì)的 API 在 jQuery 的 Templates 里,內(nèi)置的 demo 也盡情地演示了各種用法。
就我自己的幾次使用,感覺(jué)很不錯(cuò),用更加直觀方面的 HTML 寫法而不是 JavaScript 拼湊 來(lái)寫結(jié)構(gòu),然后用 JSON 變量來(lái)占位的方式來(lái)填充數(shù)據(jù),代碼看起來(lái)好多了。
Tmpl提供了幾種tag:
${}:等同于{{=}},是輸出變量,通過(guò)了html編碼的。
{{html}}:輸出變量html,但是沒(méi)有html編碼,適合輸出html代碼。
{{if }} {{else}}:提供了分支邏輯。
{{each}}:提供循環(huán)邏輯,$value訪問(wèn)迭代變量。
jquery tmpl的使用方法:
模板定義:
方法一:
<script id="movieTemplate" type="text/x-jquery-tmpl"> <li> <b>${Name}</b> (${ReleaseYear}) </li> </script>
方法二:
function makeTemplate(){ var markup='<li><b>${Name}</b> (${ReleaseYear})</li>‘; $.template(“movieTemplate”, markup); }
DATA:
var movies = [ { Name: "The Red Violin", ReleaseYear: "1998" }, { Name: "Eyes Wide Shut", ReleaseYear: "1999" }, { Name: "The Inheritance", ReleaseYear: "1976" } ];
Script:
$( "#movieTemplate" ).tmpl( movies ) .appendTo( "#movieList" );
實(shí)例1:
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.min.js"></script> <script src="http://ajax.microsoft.com/ajax/jquery.templates/beta1/jquery.tmpl.min.js"></script> </head> <body> <ul class="param-list"></ul> <script type="text/x-jquery-tmpl" id="new-param-tmpl"> <li rel="${num}"> <input type="text" name="key[${num}]" value="${key}" placeholder="key" /> = <input type="text" name="value[${num}]" value="${value}" placeholder="value" /> <button type="button" class="button small remove-param"><img src="http://mediacdn.disqus.com/1338845651/images/v3/icon/16/69.png" height="12" alt=""/></button> <button type="button" class="button small add-param"><span><img src="http://mediacdn.disqus.com/1338845651/images/v3/icon/16/13.png" height="12" alt=""/></button> </li> </script> <script> $(function(){ function addParam(key, value) { var param_list = $('.param-list'); var num = param_list.find('li').length; // build a template to clone the current row var built = $('#new-param-tmpl').tmpl({ num: num, key: key || '', value: value || '', }); if (key) param_list.prepend(built); else param_list.append(built); param_list.find('li:not(:last) .add-param').hide(); param_list.find('li:last .add-param').show(); param_list.find('li:not(:last) .remove-param').show(); param_list.find('li:last .remove-param').hide(); } // bind events $('.param-list .remove-param').live('click', function(){ $(this).parent().remove(); return false; }); $('.param-list .add-param').live('click', function(){ addParam(); return false; }); addParam(); }) </script> </body> </html>
實(shí)例2
<ul id="movieList"></ul> <script id="movieTemplate" type="text/x-jquery-tmpl"> <li><b>${Name}</b> (${ReleaseYear})</li> </script> <script type="text/javascript"> var movies = [ { Name: "The Red Violin", ReleaseYear: "1998" }, { Name: "Eyes Wide Shut", ReleaseYear: "1999" }, { Name: "The Inheritance", ReleaseYear: "1976" } ]; // Render the template with the movies data and insert // the rendered HTML under the "movieList" element $( "#movieTemplate" ).tmpl( movies ) .appendTo( "#movieList" ); </script>
相關(guān)文章
jquery文字填寫自動(dòng)高度的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇jquery文字填寫自動(dòng)高度的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11JQuery自定義事件的應(yīng)用 JQuery最佳實(shí)踐
本文主要介紹JQuery框架里面支持的自定義事件模型,通過(guò)實(shí)例說(shuō)明什么時(shí)候可以利用事件模型進(jìn)行面向?qū)ο蟮腏S編程,以及利用“帶命名空間的事件處理函數(shù)”來(lái)避免unbind時(shí)影響別的事件訂閱。2010-08-08jquery struts 驗(yàn)證唯一標(biāo)識(shí)(公用方法)
本教程將為大家詳細(xì)介紹下使用公用方法驗(yàn)證jquery struts唯一標(biāo)識(shí),感興趣的朋友可以參考下哈,希望可以幫助到你2013-03-03jQuery事件綁定.on()簡(jiǎn)要概述及應(yīng)用
前幾天看到事件委托的時(shí)候,關(guān)于live()方法講的不是很詳細(xì),就去搜了一下關(guān)于live()和delegate()的,最后看源碼發(fā)現(xiàn)bind()和delegate()都是由on()實(shí)現(xiàn)的,感興趣的朋友可以了解下,或許本文對(duì)你有所幫助2013-02-02基于Jquery的$.cookie()實(shí)現(xiàn)跨越頁(yè)面tabs導(dǎo)航實(shí)現(xiàn)代碼
基于Jquery的$.cookie()實(shí)現(xiàn)跨越頁(yè)面tabs導(dǎo)航實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-03-03JS中Array數(shù)組學(xué)習(xí)總結(jié)
本文主要介紹了JS中Array數(shù)組的相關(guān)知識(shí)。具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01jQuery 借助插件Lavalamp實(shí)現(xiàn)導(dǎo)航條動(dòng)態(tài)美化效果
導(dǎo)航條動(dòng)態(tài)顯示效果借助插件Lavalamp可以輕松實(shí)現(xiàn),以前用animate來(lái)實(shí)現(xiàn),效果不是很好2013-09-09