1. [代码]tmpl.js     
// Simple JavaScript Templating
// John Resig - http://ejohn.org/ - MIT Licensed
(function() {
var cache = {};

this.tmpl = function tmpl(str, data) {

// Figure out if we're getting a template, or if we need to
// load the template - and be sure to cache the result.
var fn = 
!/\W/.test(str) 

cache[str] = cache[str] || tmpl(document.getElementById(str).innerHTML) 
:
// Generate a reusable function that will serve as a template
// generator (and which will be cached).
new Function(
"obj", "var p=[],print=function(){p.push.apply(p,arguments);};" +

// Introduce the data as local variables using with(){}
"with(obj){p.push('" +

// Convert the template into pure JavaScript
strhttp://www.huiyi8.com/jiaoben/​
.replace(/[\r\t\n]/g, " ")
.split("<%").join("\t")
.replace(/((^|%>)[^\t]*)'/g, "$1\r")
.replace(/\t=(.*?)%>/g, "',$1,'")
.split("\t")
.join("');")
.split("%>")
.join("p.push('")
.split("\r")
.join("\\'") + 
"');}return p.join('');"
); // Function ends

// Provide some basic currying to the user
return data ? fn(data) : fn;
};
})(); 
2. [脚本代码]index.html     
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>jQuery特效
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>JavaScript tmpl Use Demo</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="./tmpl.js" type="text/javascript"></script>
                <!-- 下面是模板user_tmpl -->
<script type="text/html" id="user_tmpl">
<% for ( var i = 0; i < users.length; i++ ) { %>
<li><a href="<%=users[i].url%>"><%=users[i].name%></a></li>
<% } %>
</script>
<script type="text/javascript">
                        // 用来填充模板的数据
var users = [
{ url: "http://baidu.com", name: "jxq"},
{ url: "http://google.com", name: "william"}
];

$(function() {
                               // 调用模板引擎函数将数据填充到模板获得最终内容
$("#myUl").html(tmpl("user_tmpl", users));
});
</script>
</head>
<body>
<div>
<ul id="myUl">
</ul>
</div>
</body>
</html>

最新文章

  1. javascript中针对float的ceil及floor
  2. Linux如何下解压windows下的.zip和.rar文件
  3. 基于NPOI的报表引擎——ExcelReport
  4. Spring 4 官方文档学习(十四)WebSocket支持
  5. MongoDB 介绍
  6. CentOS 6.5 生产环境编译安装LNMP
  7. 11g添加asm
  8. [Python正则表达式] 字符串中xml标签的匹配
  9. [转载]ExtJs4 笔记(9) Ext.Panel 面板控件、 Ext.window.Window 窗口控件、 Ext.container.Viewport 布局控件
  10. ajax请求总是不成功?浏览器的同源策略和跨域问题详解
  11. SRM659 1100pts
  12. Python 集合(set)使用
  13. Rxjava2视频笔记
  14. 2017腾讯校招面试回忆(成功拿到offer)
  15. spring配置文件注入到静态变量
  16. Go语言之高级篇beego框架之layui框架应用
  17. 写一个简单脚本检测mysql主从是否正常
  18. readv与writev
  19. React Native Android启动白屏的一种解决方案下
  20. 项目解析1、登录验证用户是否存在 储备知识 Python 之 decorator装饰器

热门文章

  1. ⑨要写信(codevs 1697)
  2. json-lib maven依赖出错的问题
  3. SQLite数据库相关操作
  4. ios 处理内存警告
  5. 新闻:融资600万 他用一套系统优化15大HR工作场景 精简入转调离 月开通214家 | IT桔子
  6. postgresql 导出建表语句的方法-类似describe table
  7. android 4.4以上能够实现的沉浸式状态栏效果
  8. 设计模式入门之原型模式Prototype
  9. 使用脚本删除ios工程中未使用图片
  10. linux安装jdk tomcat nginx 以及常用命令