123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Document</title>
- </head>
- <body>
- <script>
- var someTree = {
- tagName: "table", //html tag
- subTags: [ //вложенные тэги
- {
- tagName: "tr",
- subTags: [
- {
- tagName: "td",
- text: "some text",
- },
- {
- tagName: "td",
- text: "some text 2",
- }
- ]
- }
- ],
- attrs:
- {
- border: 1,
- },
- }
- var table = someTree.tagName
- var tdchki = someTree.subTags[0].subTags
- var value = Object.keys(someTree.attrs)[0]
- debugger
- function toHtml(obj) {
- var element = document.createElement(obj.tagName);
-
- if (obj.text !== undefined) {
- element.innerHTML = obj.text
- }
- if (undefined !== obj.attrs) {
- for (let key in obj.attrs) {
- element[key] = obj.attrs[key]
- }
- }
-
- if (obj.subTags !== undefined) {
- for (let i = 0; i < obj.subTags.length; i++) {
- element.appendChild(toHtml(obj.subTags[i]));
- }
- }
- return element;
- }
- document.body.appendChild(toHtml(someTree));
- // var jsonString = "<" + table + " " + value + `= ` + someTree.attrs.border +`>\n<`
- // + someTree.subTags[0].tagName + ">\n<"
- // + tdchki[0].tagName + ">\n"
- // + tdchki[0].text + "\n</"
- // + tdchki[0].tagName + ">\n<"
- // + tdchki[1].tagName + ">\n"
- // + tdchki[1].text + "\n</"
- // + tdchki[1].tagName + ">\n</"
- // + someTree.subTags[0].tagName
- // + ">\n</" + table + ">"
- // console.log(jsonString)
- // document.write(jsonString)
- </script>
- </body>
- </html>
|