为了账号安全,请及时绑定邮箱和手机立即绑定

从 Javascript 嵌套对象生成 HTML

从 Javascript 嵌套对象生成 HTML

HUWWW 2022-01-13 10:46:06
真的需要你的帮助。我正在尝试编写一个从 javascript 对象生成 HTML 标记的函数。我的想法是我发送一个对象和一个根元素作为参数并递归地附加元素。这是代码。const struct = [  {    tag: 'div',    classes: ['container'],    innerHtml: [      {        tag: 'input',        classes: ['input'],        attributes: [          ['type', 'text'],          ['placeholder', 'Some input']        ]      },      {        tag: 'div',        classes: ['btn-block'],        innerHtml: [          {            tag: 'div',            classes: ['btn', 'btn-long'],            innerText: 'Long Button'          },          {            tag: 'div',            classes: ['btn', 'btn-big', 'btn-img'],            innerHtml: [              {                tag: 'img',                attributes: [                  ['src', 'https://www.w3schools.com/images/w3certified_logo_250.png']                ],              }            ],          }        ]      },      {        tag: 'div',        classes: ['red']      }    ]  }];const root = document.body;function create(obj, root) {  obj.forEach(o => {    const element = document.createElement(o.tag);    if (o.classes) {      const classes = o.classes;     element.classList.add(...classes);    }    if (o.attributes) {      o.attributes.forEach(a => {        element.setAttribute(a[0], a[1]);      })    }    if (o.hasOwnProperty('innerHtml')) {      element.append(create(o.innerHtml, element));    }    if (o.innerText) {      element.innerText = o.innerText    }    root.append(element);  });}create(struct, root);并且有一个结果;如您所见,该函数将文本“未定义”添加到每个元素。你能帮我修一下吗?UPD:由@CertainPerformance 和@Nina Scholz 的回答解决
查看完整描述

2 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

问题是


element.append(create(o.innerHtml, element));

但create不返回任何内容,因此undefined附加到每个元素的末尾。改为刚刚


create(o.innerHtml, element)

反而:


const struct = [{

  tag: 'div',

  classes: ['container'],

  innerHtml: [{

      tag: 'input',

      classes: ['input'],

      attributes: [

        ['type', 'text'],

        ['placeholder', 'Some input']

      ]

    },

    {

      tag: 'div',

      classes: ['btn-block'],

      innerHtml: [{

          tag: 'div',

          classes: ['btn', 'btn-long'],

          innerText: 'Long Button'

        },

        {

          tag: 'div',

          classes: ['btn', 'btn-big', 'btn-img'],

          innerHtml: [{

            tag: 'img',

            attributes: [

              ['src', 'https://www.w3schools.com/images/w3certified_logo_250.png']

            ],

          }]

        }

      ]

    },

    {

      tag: 'div',

      classes: ['red']

    }

  ]

}];


const root = document.body;


function create(obj, root) {

  obj.forEach(o => {

    const element = document.createElement(o.tag);


    if (o.classes) {

      const classes = o.classes;

      element.classList.add(...classes);

    }

    if (o.attributes) {

      o.attributes.forEach(a => {

        element.setAttribute(a[0], a[1]);

      })

    }


    if (o.hasOwnProperty('innerHtml')) {

      create(o.innerHtml, element)

    }


    if (o.innerText) {

      element.innerText = o.innerText

    }


    if (element !== undefined) {

      root.append(element);

    }

  });

}


create(struct, root);

.container {

  padding: 5px;

  border: 1px solid black;

  display: flex;

  justify-content: space-around;

  align-items: center;

}


.input {

  height: 20px;

  width: 200px;

}


.btn-block {

  display: flex;

  justify-content: space-around;

  align-items: center;

}


.btn {

  border: 1px solid black;

  border-radius: 5px;

  padding: 5px 15px;

  text-align: center;

}


.btn:hover {

  cursor: pointer;

}


.btn-long {

  width: 300px;

  margin-right: 10px;

}


.red {

  background: red;

  height: 100px;

  width: 100px;

}


查看完整回答
反对 回复 2022-01-13
?
青春有我

TA贡献1784条经验 获得超8个赞

你只需要


create(o.innerHtml, element);

没有包装element.append(/* ... */);,因为你的函数不返回东西。


function create(obj, root) {

  obj.forEach(o => {

    const element = document.createElement(o.tag);


    if (o.classes) {

      const classes = o.classes;

      element.classList.add(...classes);

    }

    if (o.attributes) {

      o.attributes.forEach(a => {

        element.setAttribute(a[0], a[1]);

      })

    }


    if (o.hasOwnProperty('innerHtml')) {

       create(o.innerHtml, element);

      //element.append();

    }


    if (o.innerText) {

      element.innerText = o.innerText

    }


    root.append(element);

  });

}



const struct = [{

  tag: 'div',

  classes: ['container'],

  innerHtml: [{

      tag: 'input',

      classes: ['input'],

      attributes: [

        ['type', 'text'],

        ['placeholder', 'Some input']

      ]

    },

    {

      tag: 'div',

      classes: ['btn-block'],

      innerHtml: [{

          tag: 'div',

          classes: ['btn', 'btn-long'],

          innerText: 'Long Button'

        },

        {

          tag: 'div',

          classes: ['btn', 'btn-big', 'btn-img'],

          innerHtml: [{

            tag: 'img',

            attributes: [

              ['src', 'https://www.w3schools.com/images/w3certified_logo_250.png']

            ],

          }],

        }

      ]

    },

    {

      tag: 'div',

      classes: ['red']

    }

  ]

}];


const root = document.body;

create(struct, root);


查看完整回答
反对 回复 2022-01-13
  • 2 回答
  • 0 关注
  • 171 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号