开发者

Compare an image in javascript

I am tryi开发者_JAVA百科ng to compare an image in javascript. If the image is true it will change to a different image. I wrote an if statement but it doesn't seem to work. Does anyone know how I can achieve this?

function test()
{
imageElement = document.getElementById('pic');
if(imageElement.src == "images/cat_12.gif"){

imageElement.src = "images/press2_12.gif";
}else{

}
}


function test(){
    if(imageElement.src.indexOf("images/cat_12.gif") != -1){
       //
    }
}


src will be converted to full URL when you read it from Javascript. You may try to extract the filename instead, e.g. img.src.substr(img.src.lastIndexOf('/')).

A better solution would be use classes and CSS background switch between images.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜