gpt4 book ai didi

javascript - New Image()应该添加.src吗?

转载 作者:行者123 更新时间:2023-12-02 19:21:14 25 4
gpt4 key购买 nike

关于new Image()的问题

我认为以下代码来自“JavaScript by Example Second Edition”

planet[0] = "images/Venus.jpg" 

错了,应该是

planet[0].src = "images/Venus.jpg" 

并替换

document.images["planet_pic"].src = planet[i]; 

document.images["planet_pic"].src = planet[i].src;

对吗?

<!DOCTYPE html>
<html>
<head>
<title>Slide Show</title>
<script type="text/javascript">
var i = 0;
var timeout;
function preLoadImages() {
if (document.images) {
planet = new Array(); // global variable
planet[0] = new Image();
planet[0] = "images/Venus.jpg";
planet[1] = new Image();
planet[1] = "images/Neptune.jpg";
planet[2] = new Image();
planet[2] = "images/Earth.jpg";
planet[3] = new Image();
planet[3] = "images/Mars.jpg";
planet[4] = new Image();
planet[4] = "images/Jupiter.jpg";
}
else {
alert("There are no images to preload");
}
}
function startSlideShow() {
if (i < planet.length) {
document.images["planet_pic"].src = planet[i];
i++;
}
else {
i = 0;
document.images["planet_pic"].src = planet[i];
}
timeout = setTimeout('startSlideShow()', 1500);
}
function stopSlideShow() {
clearTimeout(timeout);
}
</script>
</head>
<body bgcolor="black" onload="preLoadImages()">
<img name="planet_pic" src="/images/Jupiter.jpg" height="348"
width="545px" />
<form>
<br />
<br />
<input type="button" value="Start Show"
onclick="return startSlideShow();" />
<input type="button" value="Stop Show"
onclick="return stopSlideShow();" />
</form>
</body>
</html>

最佳答案

是的,应该是,

planet[0] = new Image();
planet[0].src = "images/Venus.jpg";

关于javascript - New Image()应该添加.src吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12509146/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com