简体   繁体   English

如何在知道此名称的表格中显示图像

[英]How to show image in the table having the knowing the name of this

How can I show the image in the table knowing its name?如何在知道其名称的表中显示图像?

I explain better;我解释得更好; I have a table with three fields, id, title and image.我有一个包含三个字段的表,id、title 和 image。 In the image I am shown the name.在图像中,我显示了名称。 but I need the image to be shown.但我需要显示图像。

How can I fix this?我怎样才能解决这个问题?

 let table = document.getElementById("my-table"); var xmlhttp = new XMLHttpRequest(); var url = "https://r2f5k3e89j.execute-api.us-east-2.amazonaws.com/articles"; xmlhttp.onreadystatechange = function () { var innerXmlhttp; if (this.readyState == 4 && this.status == 200) { var allart = JSON.parse(this.responseText); console.log(allart); allart.Items.forEach(item => { let child = document.createElement("tr"); child.innerHTML = ` <td>${item.id}</td> <td>${item.title}</td> <td>${item.image}</td>`; table.appendChild(child); }) } }; xmlhttp.open("GET", url, true); xmlhttp.send();
 <table id="my-table" width="90%"> <tr> <th>Id</th> <th>Title</th> <th>Image</th> </tr> </table>

You should use image tag to do so您应该使用图像标签来这样做

 let table = document.getElementById("my-table");

        var xmlhttp = new XMLHttpRequest();
        var url = "https://r2f5k3e89j.execute-api.us-east-2.amazonaws.com/articles";
        xmlhttp.onreadystatechange = function () {
            var innerXmlhttp;
            if (this.readyState == 4 && this.status == 200) {
                var allart = JSON.parse(this.responseText);
                console.log(allart);
                allart.Items.forEach(item => {
                    let child = document.createElement("tr");
                    child.innerHTML = `
                    <td>${item.id}</td>
                    <td><a href="http://google.com">${item.title}</a></td>
                    <td><img src='${item.image}' alt='Something' /></td>`;
                    table.appendChild(child);
                })
            }
        };
        xmlhttp.open("GET", url, true);
        xmlhttp.send();

You can show image by using its source url.您可以使用其源 url 显示图像。 For example, if your image url which is coming from response json is https://example.com/image/url then you can show it by using this url like that: For example, if your image url which is coming from response json is https://example.com/image/url then you can show it by using this url like that:

<img src="https://example.com/image/url"/></td>`;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM