xxx
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>手写虚拟dom</title>
|
||||
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="text/babel">
|
||||
/*@jsx createElement*/ //@jsx babeld的子执行指令
|
||||
let vDom=(<div id="app" name="chenxuan">
|
||||
<li name="world">hello world1!</li>
|
||||
<li name="kitty">hello kitty2! <span>hehe</span></li>
|
||||
</div>);
|
||||
// let vDom=("hello world");
|
||||
|
||||
function createElement(nodeName,attr,...args){
|
||||
// console.log({nodeName:nodeName,attr:attr,children:[].concat(...args)});
|
||||
// console.log(...args);
|
||||
return {nodeName:nodeName,attr:attr,children:[].concat(...args)}
|
||||
}
|
||||
|
||||
function render(vnode){
|
||||
// console.log(vnode)
|
||||
// console.log(vnode.split);
|
||||
//文本
|
||||
if(vnode.split){
|
||||
console.log(vnode)
|
||||
return document.createTextNode(vnode);
|
||||
}
|
||||
else{
|
||||
// console.log(vnode.children)
|
||||
let node=document.createElement(vnode.nodeName);
|
||||
let attr=vnode.attr || {};
|
||||
Object.keys(attr).forEach((k)=>{
|
||||
node.setAttribute(k,attr[k])
|
||||
});
|
||||
|
||||
(vnode.children || []).forEach((n)=>{
|
||||
node.appendChild(render(n))
|
||||
})
|
||||
// console.log(node);
|
||||
return node;
|
||||
}
|
||||
//非文本
|
||||
}
|
||||
let dom=render(vDom);
|
||||
document.body.appendChild(dom);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user