-
Notifications
You must be signed in to change notification settings - Fork 0
/
VirtualDOM2.html
62 lines (57 loc) · 2.04 KB
/
VirtualDOM2.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
56
57
58
59
60
61
62
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, initial-scale=1.0">
<title>Virtual DOM(二)</title>
</head>
<body>
<h1>在 Virtual DOM 的基础上给 VNode 类添加 render 方法,render 方法把一个虚拟的 DOM 节点渲染成真正的 DOM 节点,例如:</h1>
<pre>
const ul = h('ul', {id: 'list', style: 'color: red'}, [
h('li', {class: 'item'}, ['Item 1']),
h('li', {class: 'item'}, ['Item 2']),
h('li', {class: 'item'}, ['Item 3'])
]
const urlDom = ul.render() // 渲染 DOM 节点和它的子节点
ulDom.getAttribute('id') === 'list' // true
ulDom.querySelectorAll('li').length === 3 // true
</pre>
<script>
class VNode {
constructor(tagName, props, children) {
this.tagName = tagName
this.props = props
this.children = Array.isArray(children) ? children : []
this.render = this.render.bind(this)
}
createEl(tagName, props) {
const el = document.createElement(tagName);
for (const key in props) {
el.setAttribute(key, props[key])
}
return el;
}
render() {
const elWrap = this.createEl(this.tagName, this.props)
this.children.forEach(item => {
const el = item instanceof VNode ? item.render() : document.createTextNode(item);
elWrap.appendChild(el)
});
return elWrap;
}
}
function h(tagName, props, children) {
return new VNode(tagName, props, children)
}
const ul = h('ul', { id: 'list', style: 'color: red' }, [
h('li', { class: 'item' }, ['Item 1']),
h('li', { class: 'item' }, ['Item 2']),
h('li', { class: 'item' }, ['Item 3'])
])
const urlDom = ul.render()
console.log(urlDom)
</script>
</body>
</html>