javascript——浅谈javascript模版(自定义)

 1 /**
 2  * Created by Administrator on 15-1-19.
 3  */
 4 function functionUtil() {
 5 
 6 }
 7 
 8 functionUtil = {
 9     //某个DOM节点是否有某个属性
10     hasAttr: function (el, name) {
11         var attr = el.getAttributeNode && el.getAttributeNode(name);
12         return attr ? attr.specified : false
13     },
14     //根据class获取元素
15     getByClass: function (sClass, oParent) {
16         oParent = oParent || document;
17         if (!oParent.getElementsByClassName) {
18             return oParent.getElementsByClassName(sClass);
19         }
20         var arr = [];
21         var aEle = oParent.getElementsByTagName('*');
22         var reg = new RegExp('(^|\\s)' + sClass + '(\\s|$)');
23         //var reg = new RegExp('(^|[\\x20\\t\\r\\n\\f])' + sClass + '([\\x20\\t\\r\\n\\f]|$)');
24         for (var i = 0; i < aEle.length; i++) {
25             if (reg.test(aEle[i].className)) {
26                 arr.push(aEle[i]);
27             }
28         }
29         return arr;
30     },
31     //动态添加样式表
32     addSheetFile: function (path) {
33         var fileref = document.createElement("link")
34         fileref.rel = "stylesheet";
35         fileref.type = "text/css";
36         fileref.href = path;
37         fileref.media = "screen";
38         var headobj = document.getElementsByTagName('head')[0];
39         headobj.appendChild(fileref);
40     },
41     //根据指定格式如 ${name} 绑定json数据
42     LoadJsonData: function (sParent, oJson) {
43         var oParent = document.getElementById(sParent);
44         if (oJson instanceof Array) {
45             var str = oParent.innerHTML;
46             for (var i = 0; i < oJson.length - 1; i++) {
47                 oParent.innerHTML += str;
48             }
49             for (var d in oJson) {
50                 oParent.children[d].innerHTML = oParent.children[d].innerHTML.replace(/\$\{(\w+)\}/g, function (str, $1) {
51                     return oJson[d][$1] ? oJson[d][$1] : '';
52                 });
53             }
54 
55         } else {
56             oParent.innerHTML = oParent.innerHTML.replace(/\$\{(\w+)\}/g, function (str, $1) {
57                 return oJson[$1] ? oJson[$1] : '';
58             });
59         }
60     },
61     //根据指定格式如<%……%>绑定json数据
62     TemplateEngine: function (html, options) {
63         html = html.replace(/(&gt;)|(&lt;)/g, function (str, $1, $2) {
64             switch (str) {
65                 case $1:
66                     return '>';
67                 case $2:
68                     return '<';
69             }
70         });
71         var re = /<%([^%>]+)?%>/g, reExp = /(^( )?(if|for|else|switch|case|break|{|}))(.*)?/g, code = 'var r=[];\n', cursor = 0;
72         var add = function (line, js) {
73             js ? (code += line.match(reExp) ? line + '\n' : 'r.push(' + line + ');\n') :
74                 (code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : '');
75             return add;
76         }
77         while (match = re.exec(html)) {
78             add(html.slice(cursor, match.index))(match[1], true);
79             cursor = match.index + match[0].length;
80         }
81         add(html.substr(cursor, html.length - cursor));
82         code += 'return r.join("");';
83         return new Function(code.replace(/[\r\t\n]/g, '')).apply(options);
84     }
85 }

1、第一种方式:${key}

functionUtil.LoadJsonData(element, data);

”html“代码:

1 <div id="data">
2     <div class="item">
3         姓名:${name}<br/>
4         年龄:${age}<br/>
5         职业:${job}<br/><br/>
6     </div>
7 </div>

javascript代码:

 1 var data = [
 2            {
 3                     name: '徐磊',
 4                     age: 24,
 5                     job: 'IT'
 6                 },
 7                 {
 8                     name: '李磊',
 9                     age: 23,
10                     job: '翻译'
11                 }
12     ];
13 
14 
15 functionUtil.LoadJsonData('data', data);    

执行结果:

 

2、第二种方式<% 代码 %>

functionUtil.TemplateEngine(string,Object);

"html"代码:

 1 <div id="test3">
 2     <%if(this.isShow){
 3       for(var i in this.data){%>
 4         <p href="#">姓名:<%this.data[i].name%></p>
 5 
 6         <p href="#">年龄:<%this.data[i].age%></p>
 7 
 8         <p href="#">工作:<%this.data[i].job%></p>
 9         <br/>
10     <%}}%>
11 </div>

 

javascript代码:

 1 var person = {
 2                 data: [
 3                     {
 4                         name: '徐磊',
 5                         age: 24,
 6                         job: 'IT'
 7                     },
 8                     {
 9                         name: '李磊',
10                         age: 23,
11                         job: '翻译'
12                     }
13                 ],
14                 isShow: true
15             }
16 
17 
18    document.getElementById("test3").innerHTML = functionUtil.TemplateEngine(document.getElementById("test3").innerHTML, person);

 

结果:

posted @ 2015-01-28 15:27  徐大腿  阅读(366)  评论(0编辑  收藏  举报