簡體   English   中英

我有一個字符數組,我想使用 javascript 在 html 中以 4 行的形式顯示名稱、房屋和圖像。 我是foreach嗎?

[英]I have a array of characters, I want to display the name, house and image in rows of 4 in html using javascript. Do I foreach?

試過這樣的東西potterCharacters.forEach((x) => getCharacters(x.name)); 這僅顯示名稱,但似乎無法繼續。

function Init()
{
    debugger;
    potterCharacters.forEach((x) => getCharacters(x.name));
    text += "</ul>";
    document.getElementById("rows").innerHTML = text;    
}

function getCharacters(value)
{
    text += "<th>" + value + "</th>";
}```
function Init() {
    const text = potterCharacters.map(character => {
        const { name, house, image } = character
        return `<tr>
            <td>${name}</td>
            <td>${house}</td>
            <td><img src="${image}"/></td>
        </tr>`
    })
    document.getElementById("characters").innerHTML = text    
}

html:

<table id="characters"></table>

嘗試過類似potterCharacters.forEach((x) => getCharacters(x.name)); 這僅顯示名稱,但似乎無法繼續使用。

function Init()
{
    debugger;
    potterCharacters.forEach((x) => getCharacters(x.name));
    text += "</ul>";
    document.getElementById("rows").innerHTML = text;    
}

function getCharacters(value)
{
    text += "<th>" + value + "</th>";
}```

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM