javascript - how to append an element to an empty parent with null lastChild? -
from js object, i'm trying assemble element children. can please see how append element (e.g. appendchild) empty parent null lastchild or guide me better methods assemble such element (copied in below) , inject document.body? time.
z=[ {"level":0,"order":0,"t":"section","a":{"class":"ro s-14 m-3"},"first":1}, {"level":1,"order":0,"t":"h1","a":{"class":"tx-1 s-35"},"x":"team"}, {"level":1,"order":1,"t":"div","a":{"class":"tx-0"}}, {"level":2,"order":0,"t":"h3","x":"roberto carlos"}, {"level":2,"order":1,"t":"ul"}, {"level":3,"order":0,"t":"li","x":"this textnode"}, {"level":3,"order":1,"t":"li","x":"this textnode"}, {"level":3,"order":2,"t":"li","x":"this textnode"}, {"level":3,"order":3,"t":"li","x":"this textnode"}, {"level":3,"order":4,"t":"li","x":"this textnode"}, {"level":3,"order":5,"t":"li","x":"this textnode"}, {"level":3,"order":6,"t":"li","x":"this textnode"}, {"level":3,"order":7,"t":"li","x":"this textnode"}, {"level":3,"order":8,"t":"li","x":"this textnode"}, {"level":2,"order":2,"t":"li","x":"this textnode"}, {"level":1,"order":2,"t":"div","a":{"class":"tx-0"}}, {"level":2,"order":0,"t":"h3","x":"diego maradona"}, {"level":2,"order":1,"t":"ul"}, {"level":3,"order":0,"t":"li","x":"this textnode"}, {"level":3,"order":1,"t":"li","x":"this textnode"}, {"level":3,"order":2,"t":"li","x":"this textnode"}, {"level":3,"order":3,"t":"li","x":"this textnode"}, {"level":3,"order":4,"t":"li","x":"this textnode"}, {"level":3,"order":5,"t":"li","x":"this textnode"}, {"level":3,"order":6,"t":"li","x":"this textnode"}, {"level":3,"order":7,"t":"li","x":"this textnode"}, {"level":3,"order":8,"t":"li","x":"this textnode"}, {"level":3,"order":9,"t":"li","x":"this textnode"}, {"level":3,"order":9,"t":"li","x":"this textnode"}, {"level":3,"order":10,"t":"li","x":"this textnode"}, {"level":2,"order":2,"t":"li","x":"this textnode"}]; bodyo:function(z){ refs=[]; (i in z){ el=document.createelement(z[i].t); if (z[i].x!=undefined){ tx=document.createtextnode(z[i].x); el.appendchild(tx); } (j in z[i].a){ el.setattribute(j,z[i].a[j]); } if (i==0){ ref=el; refs.push(ref); }else{ yy=i;y=-(-yy-1); if (z[y]!=undefined && z[i]!=undefined){ x=(z[y].level-z[i].level); if (x==1){ refs.push(el); } } ll=z[i].level-z[i-1].level; oo=z[i].order-z[i-1].order; if (ll==0){ if (oo==0){ } if (oo==1){ if (refs[refs.length-1].lastchild==null){ refs[refs.length-1].lastchild=el; }else{ refs[refs.length-1].lastchild.appendchild(el); } } if (oo<0){ } } if(ll==1){ if (oo==0){ if (refs[refs.length-1].lastchild==null){ refs[refs.length-1].lastchild=el; }else{ refs[refs.length-1].lastchild.appendchild(el); } } if (oo==1){ } if (oo<0){ if (refs[refs.length-1].lastchild==null){ console.log(refs[refs.length-1].lastchild); console.log(el.childnodes); }else{ refs[refs.length-1].lastchild.appendchild(el); } } } if(ll<1){ if (oo==0){ } if (oo==1){ } if (oo<0){ } } } } for(g in refs){ if(g>0 && g<refs.length){ refs[0].appendchild(refs[g]) } if(g==refs.length-1){ document.body.appendchild(refs[0]); } } },
Comments
Post a Comment