123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <header>
- HTML Tree
- </header>
- <body>
- <script>
- function walker(parent) {
- let str = "";
- let tagName = parent.tagName;
- if (tagName) {
- str = `<${tagName}`;
- let attrs = parent.attrs;
- if (attrs) {
- for (const attrName in attrs) {
- str += ` ${attrName}=${attrs[attrName]}`;
- }
- }
- str += '>';
- for (const child of parent.children) {
- str += walker(child) //вложенный вызов - вложенный уровень вложенности :-D
- }
- str += `</${tagName}>`;
- }
- else {
- str = parent;
- }
- return str;
- }
- const table = {
- tagName: 'table',
- attrs: {
- border: "1",
- },
- children: [
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["1x1"],
- },
- {
- tagName: "td",
- children: ["1x2"],
- },
- ]
- },
- {
- tagName: 'tr',
- children: [
- {
- tagName: "td",
- children: ["2x1"],
- },
- {
- tagName: "td",
- children: ["2x2"],
- },
- ]
- }
- ]
- }
- document.write(walker(table)); //вернет <table border='1' ....
- </script>
- </body>
|