我正在尝试从多维数组加载图像,但我似乎无法显示图像.它们只显示为文本.
function cartoonsRndm(x) {
var cartoon = new Array();
cartoon[0] = {
person: "Trixie",
show: "my little pony",
img: "img/trixie.jpg"
};
cartoon[1] = {
person: "Sanic",
show: "Sanic the hagehug",
img: "img/sanic.png"
};
cartoon[2] = {
person: "megaman",
show: "blue bomber",
img: "img/man.jpg"
};
var hero = cartoon[x]["person"] + " " + cartoon[x]["show"] + " " + cartoon[x][src = "img"]
document.getElementById("answer").innerHTML = hero
}
Run Code Online (Sandbox Code Playgroud)
您正在使用src值连接字符串.您需要构建<img>HTML标记:
var hero = cartoon[x].person + " " + cartoon[x].show + " <img src='" + cartoon[x].img + "'>";
document.getElementById("answer").innerHTML = hero;
Run Code Online (Sandbox Code Playgroud)
另请注意,cartoon[x]["person"]相当于cartoon[x].person但更短.
| 归档时间: |
|
| 查看次数: |
35 次 |
| 最近记录: |