-
Notifications
You must be signed in to change notification settings - Fork 0
/
04-2_mustache基本使用-循环对象数组-巧妙.html
55 lines (46 loc) · 1.45 KB
/
04-2_mustache基本使用-循环对象数组-巧妙.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<div id="container">
</div>
<!--
模版: 只要 script 标签的 type 属性值
不是 text/javascript 那么就不会解析这块标签
-->
<script type="text/template" id="mytemplate">
<ul>
{{#arr}}
<li>
<div class="hd">{{name}}的信息</div>
<div class="bd">
<p>姓名:{{name}}</p>
<p>年龄:{{age}}</p>
<p>性别:{{sex}}</p>
</div>
</li>
{{/arr}}
</ul>
</script>
<script src="jslib/mustache.js"></script>
<script>
var templateStr = document.querySelector('#mytemplate').innerHTML;
var data = {
arr: [
{"name": "小明", "age": 12, "sex": "男"},
{"name": "小红", "age": 22, "sex": "女"},
{"name": "小亮", "age": 32, "sex": "男"},
]
};
var domStr = Mustache.render(templateStr, data);
console.log(domStr);
let container = document.querySelector("#container");
container.innerHTML = domStr;
</script>
</body>
</html>