如何将字符串更改为 URL 并将其作为图像导入?
How to change a string to a URL and import it as an image?
我在想这样的事情:
let img_1 = "./img/image.png";
let player = 1;
document.getElementById("player_img").src = "img_" + player;
然后就可以在网页上显示./img/image.png
但是上面的代码不起作用,正确的代码是什么?
最好的方法可能是使用数组。
像这样
let img_ = ["./img/image.png","./img/image.png","./img/image.png"];
let player = 1;
document.getElementById("player_img").src = img_[player - 1];
我在想这样的事情:
let img_1 = "./img/image.png";
let player = 1;
document.getElementById("player_img").src = "img_" + player;
然后就可以在网页上显示./img/image.png
但是上面的代码不起作用,正确的代码是什么?
最好的方法可能是使用数组。
像这样
let img_ = ["./img/image.png","./img/image.png","./img/image.png"];
let player = 1;
document.getElementById("player_img").src = img_[player - 1];