无法使用JavaScript更改图像src

use*_*029 -4 html javascript

我知道有人问过这个问题; 而且我已经阅读了大部分被问到的问题都无济于事.我还是遇到了麻烦.我想单击图像并更改图像源(因此图像更改).

这是我的HTML:

 <img id="picture1" onclick="showHide('builder1'); change('picture1');" width="25" height="25" src="pictures/down.png">
Run Code Online (Sandbox Code Playgroud)

忽略"showHide('builder1');".它正在发挥作用.我需要帮助改变('picture1').

这是我的Javascript:

function change(id)
{
   var isDown = true;
   var picture = document.getElementById(id);
   if(isDown === true)
   {
      picture.src = "pictures/pullup.png";
      isDown = false; //No longer a down arrow...
   }
   else
   {
      picture.src = "pictures/down.png";
      isDown = true;
   }
}
Run Code Online (Sandbox Code Playgroud)

我能够将图片更改一次(到'pictures/pullup.png'),但无法将其更改回来.我还在if/else语句的某些点添加了警报,以查看它在哪里; 它甚至从未达到声明中的"其他"部分.

Mig*_*ork 7

移动var isDown = true;到函数范围之外 - 在函数之前定义它.

这样,每次运行该函数时,都将变量设置为truefirst.


基于迈克的评论:你也可以使用这样的东西:

picture.src = "pictures/"
               + (picture.src=="pictures/pullup.png" ?
                                                     "pulldown.png"
                                                     : "pullup.png");
Run Code Online (Sandbox Code Playgroud)

如果您不喜欢三元,请使用IF.