Created
October 3, 2014 03:15
-
-
Save riconegri/bfc5e12e566cfa0e9cc4 to your computer and use it in GitHub Desktop.
write a clean html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var json = { | |
"div": { | |
"_style": "backgroung-color:#DC0600", | |
"_class": "btn btn-default", | |
"inner": { | |
span: { | |
_class: "warning", | |
inner: "bacana" | |
}, | |
b: { | |
inner: "sim" | |
} | |
} | |
} | |
}; | |
function json2html(obj) { | |
'use strict'; | |
var x, y, body; | |
for (x in obj) { | |
if (obj.hasOwnProperty(x)) { | |
body = $('<' + x + '></' + x + '>'); | |
for (y in obj[x]) { | |
if (obj[x].hasOwnProperty(y)) { | |
if (y.charAt(0) === '_') { | |
body.attr(y.replace('_', ''), obj[x][y]); | |
} | |
if (obj[x].inner instanceof Object) { | |
body.append(json2html(obj[x].inner)); | |
} else { | |
body.html(obj[x].inner); | |
} | |
} | |
} | |
} | |
} | |
console.log(body); | |
return body; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment