zoukankan      html  css  js  c++  java
  • createElement

    templet = {
                type:"div",
                attr:{ id:"div2"},
                classList:["2"],
                children:[
                    {
                        type:"span",
                        attr:{ id:"text1", value:"is text"},
                        classList:["1"],
                        id:"span1",
                        events:{},
                        children:[
                            {
                                type:"span",
                                attr:{ id:"son1", value:"is son1"},
                                classList:["1"],
                                id:"son1",
                                events:{},
                            }
                        ]
                    },
                    {
                        type:"p",
                        attr:{ id:"p1", value:"is p"},
                        classList:["1"],
                        id:"p1",
                        events:{},
                        children:[
                            {
                                type:"p",
                                attr:{ id:"p2", value:"is son2"},
                                classList:["1"],
                                id:"son2",
                                events:{},
                            }
                        ]
                    },
                ],
                id:"div2",
                events:{}
            }
            function createDOM(obj){
                const {type,attr,children,id} = obj
                let root = document.createElement(type)
                console.log(root)
                if(attr){
                    let key = Object.keys(attr)
                    key.forEach(item => {
                        if(item !== 'value'){
                            root.setAttribute(item,attr[item])
                        }else if(item === 'value'){
                            root.innerText = attr[item]
                        }
                    });
                }
                if(children){
                    let child = Object.values(children)
                    for(let i=0;i<child.length;i++){
                        let c = createDOM(child[i])
                        root.appendChild(c)
                    }
                    return root
                }else{
                    return root
                }
            }
            let c = createDOM(templet)
            console.log(c)
            document.body.appendChild(c)
  • 相关阅读:
    java 基础知识
    winform判断一个事件是否已经绑定了事件处理函数
    优质文章
    优质博客
    缩略图的创建
    记录一次错误处理 (xml序列化和反序列化相关)
    文件同步软件
    博客园优质博主集锦
    不错的博文集锦
    cesiumjs
  • 原文地址:https://www.cnblogs.com/MDGE/p/14349075.html
Copyright © 2011-2022 走看看