实现所需功能的最简单方法是保存对象,而不是数组中的字符串。这样,您可以创建具有
title
字段和an
imageUrl
类似这样的字段:
var myarray= new Array(
{
title: "The Santa Claus",
imageUrl: "https://upload.wikimedia.org/wikipedia/en/thumb/e/e7/Video-x-generic.svg/90px-Video-x-generic.svg.png"
},
{
title: "Random Movie",
imageUrl: "https://upload.wikimedia.org/wikipedia/commons/thumb/a/ad/BolexH16.jpg/220px-BolexH16.jpg"
},
// you can add as many comma-separated objects as you want
);
添加
img
元素并为其指定id
picture
. 在JS中,您可以这样设置电影的标题和图像:
document.getElementById("message").innerHTML=random.title;
// the element with id picture is an image element
document.getElementById("picture").src = random.imageUrl;
我修改了你的密码笔
here
随着提议的变化。