1.用js對象來表達dom結構html
tagName 標籤名
props 元素屬性
key 惟一標識
children 子元素,格式和父元素同樣
count 有幾個子元素,用於計算當前元素的索引,處於整個dom中的第幾個,方便dom操做node
{ "tagName": "div", "props": { "id": "container" }, "children": [ { "tagName": "h1", "props": { "style": "color:red" }, "children": [ "simple virtual dom" ], "count": 1 }, { "tagName": "p", "props": {}, "children": [ "hello world" ], "count": 1 }, { "tagName": "ul", "props": {}, "children": [ { "tagName": "li", "props": {}, "children": [ "item #1" ], "count": 1 }, { "tagName": "li", "props": {}, "children": [ "item #2" ], "count": 1 } ], "count": 4 } ], "count": 9 }
2.原js對象渲染成dom結構json
<div id="container"> <h1 style="color: red;">simple virtual dom</h1> <p>hello world</p> <ul> <li>item #1</li> <li>item #2</li> </ul> </div>
3.修改原js對象框架
{ "tagName": "div", "props": { "id": "container2" }, "children": [ { "tagName": "h5", "props": { "style": "color:red" }, "children": [ "simple virtual dom" ], "count": 1 }, { "tagName": "p", "props": {}, "children": [ "hello world2" ], "count": 1 }, { "tagName": "ul", "props": {}, "children": [ { "tagName": "li", "props": {}, "children": [ "item #1" ], "count": 1 }, { "tagName": "li", "props": {}, "children": [ "item #2" ], "count": 1 }, { "tagName": "li", "props": {}, "children": [ "item #3" ], "count": 1 } ], "count": 6 } ], "count": 11 }
4.對比哪些節點被修改
type 類型,0爲標籤名改變,1爲子元素改變(刪除或添加),2爲屬性改變,3爲內容改變
key 對象第一層中key值表示索引,原dom中第幾個元素髮生變化dom
{ "0": [ { "type": 2, "props": { "id": "container2" } } ], "1": [ { "type": 0, "node": { "tagName": "h5", "props": { "style": "color:red" }, "children": [ "simple virtual dom" ], "count": 1 } } ], "4": [ { "type": 3, "content": "hello world2" } ], "5": [ { "type": 1, "moves": [ { "index": 2, "item": { "tagName": "li", "props": {}, "children": [ "item #3" ], "count": 1 }, "type": 1 } ] } ] }
5.渲染修改後的js對象spa
a.標籤名改變,直接從新渲染整個元素,包括元素下的子元素 b.子元素改變,該刪除的刪除,該添加的添加(針對列表框架有一套本身的計算方法,能夠自行百度去研究) c.屬性改變,操做對應元素的屬性 d.內容改變,操做對應元素的內容
<div id="container2"> <h5 style="color: red;">simple virtual dom</h5> <p>hello world2</p> <ul> <li>item #1</li> <li>item #2</li> <li>item #3</li> </ul> </div>