0

I'm having a problem getting the condition in this if statement to work, i've tried getElementById & src.indexOf but I am still very new to this. I am trying to replace the blank/ball image with the x/pikachu image only if the blank image is currently showing, so if there was one of the other images there it wouldn't replace it. It doesn't seem to recognize the condition I have in there as true, any idea why? i've also tried:document.getElementById(bn).src=blank & document.images[bn].src==blank & document.pokemon.bn.src==blank looking for a way to verify if the current source of the image is the var blank. thank you

 var x = "pikachu.jpg";
 var o = "Meowth.jpg";
 var blank = "ball.jpg";             

 function b1Move(imageName){
 temp2=imageName;
 if(document.pokemon[temp2].src==blank)  
 document.pokemon[temp2].src=x;
 cMove();
 }

and the html for the images looks like this:

 <a href="javascript:b1Move('b1')"><img src="ball.jpg" height=150 width=150 name=b1         
 id =b1  > </a>
4

2 回答 2

2

src属性提供完全解析的 URL。您可能想要使用该src属性,该属性保持不变:

if( document.pokemon[temp2].getAttribute("src") == blank)
    document.pokemon[temp2].setAttribute("src",x);
于 2013-07-29T04:06:55.283 回答
0

你应该试试

if(document.pokemon[temp2].src.indexOf(blank) != -1)

因为 src 是完整的 url.. 那么你可以检查它是否包含你的图像名称

于 2013-07-29T04:15:54.633 回答