[英]Why isn't this image changing function working?
Here is what I wrote: 这是我写的:
var img_current = 0;
var image0 = new Image()
image0.src = "img/image_centrale/1_house_0.jpg"
var image1 = new Image()
image1.src = "img/image_centrale/1_house_1.jpg"
var image2 = new Image()
image2.src = "img/image_centrale/1_house_2.jpg"
function affich_centrale()
{
if (house_bought == 1 && neighborhood_bought == 0)
{
img_current = 1;
document.images.main.src = eval("image" + img_current + ".src").innerHTML = 'image_centrale';
}
else if (house_bought == 2 && neighborhood_bought == 0)
{
img_current = 2;
document.images.main.src = eval("image" + img_current + ".src").innerHTML = 'image_centrale';
}
else{}
This will go on, but I think you get the gist. 这将继续,但是我认为您的要旨是。 What's wrong with my code? 我的代码有什么问题? The images won't change as my "house_bought" variable changes. 当我的“ house_bought”变量更改时,图像不会更改。
In my HTML part, I simply put this: 在我的HTML部分中,我只是简单地这样:
<div id="image_centrale">
<img src="img/image_centrale/1_house_0.jpg" name="main" />
</div>
Note: This is an upcoming full html/js game website. 注意:这是即将发布的完整html / js游戏网站。
Why do you use src
and innerHTML
at the same time? 为什么同时使用src
和innerHTML
? src
is all you need, and there is no need for eval
as well. src
是您所需要的,并且也不需要eval
。 Try this: 尝试这个:
<div id="image_centrale">
<img src="" name="main" />
</div>
<script type="text/javascript">
var img_current = 0;
var house_bought = 1;
var neighborhood_bought = 0;
var image0 = new Image()
image0.src = "img/image_centrale/1_house_0.jpg"
var image1 = new Image()
image1.src = "img/image_centrale/1_house_1.jpg"
var image2 = new Image()
image2.src = "img/image_centrale/1_house_2.jpg"
function affich_centrale() {
if (house_bought == 1 && neighborhood_bought == 0) {
img_current = 1;
document.images.main.src = image0.src;
}
else if (house_bought == 2 && neighborhood_bought == 0) {
img_current = 2;
document.images.main.src = image1.src;
}
else { }
}
affich_centrale();
</script>
In this code example, I hard-coded the values for the variables just to make it a working example, so you will always see the first image. 在此代码示例中,我仅对变量的值进行了硬编码,以使其成为一个有效的示例,因此您将始终看到第一张图像。 Change it to suite your needs. 根据您的需要进行更改。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.