Compare an image in javascript

故事扮演 提交于 2020-06-26 14:52:30

问题


I am trying 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{

}
}

回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/5922419/compare-an-image-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!