这里是我写的:为什么这个图像改变功能不起作用?
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{}
这将继续下去,但我觉得你得到的要点。我的代码有什么问题?图像不会随着我的“house_bought”变量而改变。
在我的HTML部分,我只是把这个:
<div id="image_centrale">
<img src="img/image_centrale/1_house_0.jpg" name="main" />
</div>
注:这是即将到来的全功能HTML/JS游戏网站。
...'eval'?你知道,这会杀死小猫和小狗。但是要回答你的问题,'someImgElm.src'将是一个_String_,并且_String_上没有_innerHTML_。最重要的是,你使用了'='两次,所以就像你试图将_LHS_和_centre_部分设置为_RHS_(''image_centrale'')。此外,我没有看到“house_bought”和“neighborhood_bought”正在设置的位置,因此无论如何,您可能总是以“else {}”结尾。 –
对不起,我应该已经更精确了,house_bought和'罩都设置之前。但是,正如我测试过的(警告按钮提醒我发生变化),函数“kind of”有效,因为它告诉我“img_current = 1”,当条件满足时,它也是2。 所以,我猜这是document.images(...)部分谁不能正常工作(我一定写错了) – user3455921