开发者

Compare an image in javascript

开发者 https://www.devze.com 2023-03-03 09:33 出处:网络
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

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

精彩评论

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