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

如何在html中连接img src

如何在html中连接img src

摇曳的蔷薇 2023-10-24 21:20:03
我的目标是创建一个包含许多图像的表格。通过这样做,我将遍历一个包含该图像位置的数组。我的问题是,如何将此字符串转换为动态字符串: <td><img src="http://openweathermap.org/img/w/ + local.weather[0].icon +.png"></td>“local.weather[0].icon”是动态部分。我似乎无法找到一种方法来实现这一点。
查看完整描述

2 回答

?
大话西游666

TA贡献1817条经验 获得超14个赞

使用模板字符串。


here is the example,

var val = 'fortnite'

var x = 'I play ${val}';

console.log(x);     // I play fortnite

val = 'pubg'

console.log(x);     // I play pubg

//so I have made that dynamic using template strings. Use ` key instead for quotes.

[![This is the key][1]][1]


查看完整回答
反对 回复 2023-10-24
?
汪汪一只猫

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

你必须在js中手动完成它。(您无法在页面加载时动态添加 src 属性。为此,您必须在服务器上执行此操作或在窗口加载时通过 js 更改它)尝试下面的代码。

<td><img id="weatherImg"></td>
window.addEventListener('load', () => {
    document.getElementById("weatherImg").src = `http://openweathermap.org/img/w/${local.weather[0].icon}.png`;
});


查看完整回答
反对 回复 2023-10-24
  • 2 回答
  • 0 关注
  • 67 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信