javascript - 遍历 JSON 并获取每个数据键的图像
问题描述
我有这张桌子
<table id="player_info">
<tr>
<th>Position</th>
<th>Club</th>
<th>Points</th>
<th>Matches Played</th>
<th>Wins</th>
<th>Draws</th>
<th>Losses</th>
<th>Goals For</th>
<th>Goals Against</th>
<th>Goal Difference</th>
<th>Form</th>
</tr>
</table>
我有这个函数,它遍历 JSON 键并使用每个键创建一个新的表格单元格。我想让每个俱乐部的徽标出现在表格单元格中的名称旁边。徽标 URL 存储在 JSON 中。
我当前的代码
data.api.standings[0].forEach(elements => {
var tr = document.createElement('tr');
var img = new Image();
img.src = elements.logo;
tr.innerHTML = '<td>' + elements.rank + '</td>' +
'<td>' + elements.teamName + img + '</td>' +
'<td>' + elements.points + '</td>' +
'<td>' + elements.all.matchsPlayed + '</td>' +
'<td>' + elements.all.win + '</td>' +
'<td>' + elements.all.draw + '</td>' +
'<td>' + elements.all.lose + '</td>' +
'<td>' + elements.all.goalsFor + '</td>' +
'<td>' + elements.all.goalsAgainst + '</td>' +
'<td>' + elements.goalsDiff + '</td>' +
'<td>' + elements.forme + '</td>';
table.appendChild(tr);
});
但是我得到了团队名称和 [object HTMLImageElement] 并且没有图像。
解决方案
在你的情况下,你可以这样做
var img = '<img src="' + elements.logo + '" />'
而不是制作一个 img 元素
推荐阅读
- ms-access - MS Access 中的多表查询不可更新
- javascript - 如何编辑我的 JSX 过滤器并包含附加条件?
- gitlab - Gitlab MultiProject 管道 - 验证调用作业
- polylang - Polylang 插件不翻译报纸字符串
- python - 启动错误代码 8 以在 xcode 上构建 python 或 python3
- javascript - React Component List 过滤时无法正确更新 Input 组件的默认值
- javascript - React UI 不会在 redux 存储更改时更新
- python - 试图理解 BeautifulSoup 中的 find_all 或 findAll 类
- javascript - 如何在反应中处理“居中”您的网站
- php - PHP 从嵌套的 JSON 数组中检索值