jTemplates 使用

jTemplates是一个基于JQuery的模板引擎插件,功能强大,有了他你就再不用为使用JS绑定数据集时发愁了。

使用Templates就感觉用jstl标签编写jsp程序,再配合一些缓存插件,对性能提高有很大帮助。

再配合   delegate事件处理方法可以写非常简洁的js程序。

  jTemplates官网地址 官网的文档写得非常的

http://jtemplates.tpython.com/#results

 

 jQuery的.bind()、.live()和.delegate()之间的区别请看这里

 http://article.yeeyan.org/view/213582/179910

 

 Html代码

  • <?xml version="1.0" encoding="UTF-8" ?>  
  • <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  • <html xmlns="http://www.w3.org/1999/xhtml">  
  • <head>  
  • <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  • <title>jTemplates</title>  
  • </head>  
  • <body>  
  •     <div id="result"></div>  
  •     <div id="foreachResult"></div>  
  • </body>  
  • <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>  
  • <script src="http://jtemplates.tpython.com/jquery-jtemplates_uncompressed.js"></script><br>  
  • <!-- 这里使用一个 script标签内容来存储显示模板,这也现在大多数js模板的做法 foreach的用法--><br><script type="text/template" id="foreach">  
  •     <table>  
  •     <thead>  
  •         <tr>  
  •             <td>Index</td>  
  •             <td>Iterator</td>  
  •             <td>Name</td>  
  •             <td>Age</td>  
  •             <td>First?</td>  
  •             <td>Last?</td>  
  •         </tr>  
  •     </thread>  
  •     <tbody>  
  •         {#foreach $T.table as record begin=1}  
  •         <tr>  
  •             <td>{$T.record$index}</td>  
  •             <td>{$T.record$iteration}</td>  
  •             <td>{$T.record.name}</td>  
  •             <td>{$T.record.age}</td>  
  •             <td>{$T.record$first}</td>  
  •             <td>{$T.record$last}</td>  
  •         </tr>  
  •         {#/for}  
  •     </tbody>  
  • </table>  
  • </script>  
  • <script>  
  • //$("#result").setTemplate("Template by {$T.bold()} version <em>{$Q.version}</em>.");  
  • //$("#result").processTemplate("jTemplates");  
  • $(function($){  
  •     var data = {  
  •      name: 'User list',  
  •      list_id: 6,  
  •      table: [  
  •       {id: 1, name: 'Anne', age: 22, mail: 'anne@domain.com'},  
  •       {id: 2, name: 'Amelie', age: 24, mail: 'amelie@domain.com'},  
  •       {id: 3, name: 'Polly', age: 18, mail: 'polly@domain.com'},  
  •       {id: 4, name: 'Alice', age: 26, mail: 'alice@domain.com'},  
  •       {id: 5, name: 'Martha', age: 25, mail: 'martha@domain.com'}  
  •      ]  
  •     };  
  •     var mydata = { name: "Anne", age: "20" };  
  •       
  •     $("#result").setTemplate("{#if $T.list_id == 4} good {#elseif $T.list_id == 5} normal {#else} bad {#/if}");  
  •       
  •     $("#result").processTemplate(data);  
  •       
  •     $('#foreachResult').setTemplate($('#foreach').html()).processTemplate(data);  
  •     $('#foreachResult').delegate('td','click',function(){  
  •         alert($(this).text());  
  •     });  
  • });  
  • </script>  
  • </html> 

转载于:https://www.cnblogs.com/guozhe/archive/2012/02/15/2352622.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值