从 fetch api 将数据放入 tbody

问题描述 投票:0回答:1

您好,我想将来自 api 的数据放入表的 tbody 中,但是当我使用下面的代码时。 它为每个数据创建一个 tbody 标签。 我希望所有数据都在一个 tbody 标记中。 我该怎么办?

fetch(get_data)
.then(function (response) {
    response.text().then(function (responseText) {
        var obj = JSON.parse(responseText);



        obj.forEach(user => {
            const markup = `

                    <td class="customer" id="company${user.id}">${user.company}</td>
                    <td class="customer" id="first_name${user.id}">${user.first_name}</td>
                    <td class="customer" id="last_name${user.id}">${user.last_name}</td>
                
            


            `;
            document.querySelector("thead").insertAdjacentHTML('afterend', markup);




        });





    });





});

javascript html-table html-tbody
1个回答
1
投票

改变

document.querySelector("thead").insertAdjacentHTML('afterend', markup);

document.querySelector("tbody").insertAdjacentHTML('beforeend', markup);
© www.soinside.com 2019 - 2024. All rights reserved.