bluebelt.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <script>
  10. var someTree = {
  11. tagName: "table", //html tag
  12. subTags: [ //вложенные тэги
  13. {
  14. tagName: "tr",
  15. subTags: [
  16. {
  17. tagName: "td",
  18. text: "some text",
  19. },
  20. {
  21. tagName: "td",
  22. text: "some text 2",
  23. }
  24. ]
  25. }
  26. ],
  27. attrs:
  28. {
  29. border: 1,
  30. },
  31. }
  32. function toHtmlStr(obj) {
  33. var element = ""
  34. if (obj.tagName !== undefined){
  35. element+= "<" + obj.tagName
  36. if (obj.attrs !== undefined) {
  37. let attrs = obj.attrs
  38. for (let key in attrs) {
  39. element += ` ${key}=${attrs[key]}` + ">\n"
  40. }
  41. } else {
  42. element += + ">\n"
  43. }
  44. if (obj.text !== undefined) {
  45. element += `${obj.text}`
  46. }
  47. if (obj.subTags !== undefined) {
  48. let children = obj.subTags
  49. for (let i = 0; i < children.length; i++) {
  50. let obj = children[i]
  51. if (obj.tagName !== undefined){
  52. element+= "<" + obj.tagName
  53. if (obj.attrs !== undefined) {
  54. let attrs = obj.attrs
  55. for (let key in attrs) {
  56. element += `${key}=${attrs[key]}` + ">\n"
  57. }
  58. } else {
  59. element += ">\n"
  60. if (obj.text !== undefined) {
  61. element += `${obj.text}`
  62. }
  63. if (obj.subTags !== undefined) {
  64. let children = obj.subTags
  65. for (let i = 0; i < children.length; i++) {
  66. let obj = children[i]
  67. if (obj.tagName !== undefined){
  68. element+= "<" + obj.tagName
  69. if (obj.attrs !== undefined) {
  70. let attrs = obj.attrs
  71. for (let key in attrs) {
  72. element += `${key}=${attrs[key]}` + ">\n"
  73. }
  74. } else {
  75. element += ">\n"
  76. }
  77. if (obj.text !== undefined) {
  78. element += `${obj.text}`
  79. }
  80. element+= `</${obj.tagName}>\n`
  81. }
  82. }
  83. }
  84. element+= `</${obj.tagName}>\n`
  85. }
  86. }
  87. }
  88. element+= `</${obj.tagName}>\n`
  89. }
  90. }
  91. return document.write(element)
  92. }
  93. toHtmlStr(someTree)
  94. function toHtml(obj) {
  95. var element = document.createElement(obj.tagName);
  96. if (obj.text !== undefined) {
  97. element.innerHTML = obj.text
  98. }
  99. if (undefined !== obj.attrs) {
  100. for (let key in obj.attrs) {
  101. element[key] = obj.attrs[key]
  102. }
  103. }
  104. if (obj.subTags !== undefined) {
  105. for (let i = 0; i < obj.subTags.length; i++) {
  106. element.appendChild(toHtml(obj.subTags[i]));
  107. }
  108. }
  109. return element;
  110. }
  111. // document.body.appendChild(toHtml(someTree));
  112. </script>
  113. </body>
  114. </html>