Code implementation:
vardata=[{"tag": "UL", "attr": {"Class": "Ban_nav", "id": "Ban"}, "Child":[ {"Tag": "Li", "Child":[ {"Tag": "A", "attr": {"href": "activenet.html", "target": "_blank"}, "Child":[ {"Text": "First", "tag": "Span"}, {"Text": "Second", "tag": "Span"}, {"Text": "Third", "tag": "Span"}, ] }, {"Tag": "A", "attr": {"href": "activenet.html", "target": "_blank"}, "Child":[ {"Tag": "IMG", "attr": {"src": "Images/banner5.png"}}, {"Tag": "IMG", "attr": {"src": "Images/banner5.png"}}, {"Tag": "IMG", "attr": {"src": "Images/banner5.png"}} ] } ] }, {"Tag": "Li", "Child":[ {"Tag": "A", "attr": {"href": "activenet.html", "target": "_blank"}, "Child":[ {"Tag": "IMG", "attr": {"src": "Images/activenet/banner.png"}} ] } ] } ] }, {"Tag": ' A '} ] $(function() {SetData (' Banner ', data); }) functionSetData (name,data) {varl=$ (' <span/> ')); CreateDOM (data,l) $ (' Coder[name= ' ' +name+ ' "] '). HTML (l); } /** Infinite Level creation DOM node * @param {object} opt Parameter Object * @param {object} parent container*/ functionCreateDOM (opt,parent) {$.each (opt,function(k,v) {var_e={},l; /*when there is content to be assigned, some are not as follows: IMG*/v[' Text ' && (_e.text=v[' text ')]); /*assignment occurs when an attribute is present, with key/value pairing*/v[' attr ' && (_e=$.extend ({},_e,v[' attr ')])); /*creating a DOM node*/L=v.tag? $ (' < ' +v[' tag ']+ '/> ', _e): v[' text ']; Parent.append (l); /*recursive when a child node is present*/ if(v[' child ']) {createdom (v[' Child '],l); } }); }
View Code
HTML section:
<coder name= "banner" ></coder>
jquery infinite level create DOM node