Javascript 如何使图像仅在我的if/else语句为true时显示?

Javascript 如何使图像仅在我的if/else语句为true时显示?,javascript,Javascript,如果特定的if/else语句为true,我如何生成特定的映像 我一直在尝试,但是当if语句为false时,src只会使图像留在页面上,而不会消失 if (B == "k k"){ //I want my image to appear through this if statement }else { //and a separate image to appear if this "else" is true } 通过设置display:none,可以有条件地显示或隐藏图像

如果特定的if/else语句为true,我如何生成特定的映像

我一直在尝试,但是当if语句为false时,src只会使图像留在页面上,而不会消失

if (B == "k k"){

  //I want my image to appear through this if statement

  }else {

  //and a separate image to appear if this "else" is true
}

通过设置
display:none,可以有条件地显示或隐藏图像在其上,示例代码段如下:

函数切换图像(){
var img=document.querySelector('img');
如果(img.style.display==='block'){
img.style.display='none';
}否则{
img.style.display='block';
}
}
按钮{
显示:块;
利润率:20px0;
}
切换图像
将id添加到图像中

  <img id="your-image-id" src="" alt="" >

您已经显示了if/else。。。但不是你在每种情况下都在做什么。。。那么,我们如何才能提供帮助呢?您是否不只是引用image元素,然后将src设置为image?请参见以下内容
 var img = document.getElementById("your-image-id")

    if (B == "k k"){

   img.src ="linkto your image" 

  }else {

   img.src ="linkto your another image" 
}