-
Notifications
You must be signed in to change notification settings - Fork 0
/
1-1-template.html
38 lines (36 loc) · 1.04 KB
/
1-1-template.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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>webcomponents</title>
</head>
<body>
<div id="host"></div>
<template id="profileTemplate">
<div class="profile">
<img src="" class="profile__img">
<div class="profile__name"></div>
<div class="profile__social"></div>
</div>
</template>
<script>
function supportsTemplate() {
return 'content' in document.createElement('template');
}
if (supportsTemplate()) {
// 支持
console.log('支持');
} else {
// 不支持
console.log('不支持');
}
var template = document.querySelector('#profileTemplate');
template.content.querySelector('.profile__img').src = 'profile.png';
template.content.querySelector('.profile__name').textContent = 'Barack Obama';
template.content.querySelector('.profile__social').textContent = 'Follow me on Twitter';
// document.body.appendChild(template.content);
var clone = document.importNode(template.content, true);
document.querySelector('#host').appendChild(clone);
</script>
</body>
</html>