bluebelt.html 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. var table = someTree.tagName
  33. var tdchki = someTree.subTags[0].subTags
  34. var value = Object.keys(someTree.attrs)[0]
  35. debugger
  36. function toHtml(obj) {
  37. var element = document.createElement(obj.tagName);
  38. if (obj.text !== undefined) {
  39. element.innerHTML = obj.text
  40. }
  41. if (undefined !== obj.attrs) {
  42. for (let key in obj.attrs) {
  43. element[key] = obj.attrs[key]
  44. }
  45. }
  46. if (obj.subTags !== undefined) {
  47. for (let i = 0; i < obj.subTags.length; i++) {
  48. element.appendChild(toHtml(obj.subTags[i]));
  49. }
  50. }
  51. return element;
  52. }
  53. document.body.appendChild(toHtml(someTree));
  54. // var jsonString = "<" + table + " " + value + `= ` + someTree.attrs.border +`>\n<`
  55. // + someTree.subTags[0].tagName + ">\n<"
  56. // + tdchki[0].tagName + ">\n"
  57. // + tdchki[0].text + "\n</"
  58. // + tdchki[0].tagName + ">\n<"
  59. // + tdchki[1].tagName + ">\n"
  60. // + tdchki[1].text + "\n</"
  61. // + tdchki[1].tagName + ">\n</"
  62. // + someTree.subTags[0].tagName
  63. // + ">\n</" + table + ">"
  64. // console.log(jsonString)
  65. // document.write(jsonString)
  66. </script>
  67. </body>
  68. </html>