简体   繁体   English

按钮上的Java交换图片点击

[英]Javascript Swap Images on Button Click

I currently have 4 buttons that should swap from one image to another once clicked. 我目前有4个按钮,一旦单击应从一个图像交换到另一个图像。 I can get the first two to work, but once I click the third and fourth, once I click another button the images don't disappear - they stay on the screen. 我可以使前两个起作用,但是一旦单击第三和第四个,单击另一个按钮,图像就不会消失-它们停留在屏幕上。

I have the current code in the document head: 我在文档头中有当前代码:

 function showImg(strShow, strHide) { document.getElementById(strShow).style.display = 'block'; document.getElementById(strHide).style.display = 'none'; } 
 <div id="image1"> <INPUT type="submit" class="myButton1" value="" button onclick="showImg( 'a', 'b', 'c', 'd' )"></button> <img id="a" src="Website Photos/scannedimages/remembranceivy.jpg" width="220" height="116.895" style="display:none" alt="A" /> </div> <div id="image2"> <INPUT type="submit" class="myButton2" value="" button onclick="showImg( 'b', 'a', 'c', 'd' )"></button> <img id="b" src="Website Photos/scannedimages/remembrancemaple.jpg" width="220" height="116.895" style="display:none" alt="B" /> </div> <div id="image3"> <INPUT type="submit" class="myButton3" value="" button onclick="showImg( 'c', 'a', 'b', 'd' )"></button> <img id="c" src="Website Photos/scannedimages/remembranceoak.jpg" width="220" height="116.895" style="display:none" alt="C" /> </div> <div id="image4"> <INPUT type="submit" class="myButton4" value="" button onclick="showImg( 'd', 'a', 'c', 'd')"></button> <img id="d" src="Website Photos/scannedimages/remembrancepine.jpg" width="220" height="116.895" style="display:none" alt="D" /> </div> 

I'm totally lost. 我完全迷路了。 Does something need added to the Javascript to get the third and fourth images to disappear? 是否需要向Javascript中添加某些内容才能使第三张和第四张图片消失? Any help would be appreciated! 任何帮助,将不胜感激!

your function accepts two parameters ( strShow, strHide ) but you are passing it 4 ( 'c', 'a', 'b', 'd' ). 您的函数接受两个参数(strShow,strHide),但您要传递四个参数('c','a','b','d')。 so its ignoring the last 2 and thats why your photos arent disappearing. 因此忽略了最后2张,这就是为什么您的照片没有消失的原因。 eg when you click on image 3, image C (the first param) is shown and image B (second param) is hidden. 例如,当您单击图像3时,将显示图像C(第一个参数),而图像B(第二个参数)被隐藏。 but if the photo being displayed is D or A, it wont hide it. 但如果显示的照片是D或A,则不会将其隐藏。

Try 尝试

<div id="image3">
<INPUT type="submit" class="myButton3" value="" button onclick="showImg('c', 'd')"></input>  
<img id="c" src="Website Photos/scannedimages/remembranceoak.jpg" width="220" height="116.895" style="display:none" alt="C" /> 
</div>

<div id="image4">
<INPUT type="submit" class="myButton4" value="" button onclick="showImg('d', 'a')"></input>  
<img id="d" src="Website Photos/scannedimages/remembrancepine.jpg" width="220" height="116.895" style="display:none" alt="D" /> 
</div>

Note showIgm only takes two parameters. 注意showIgm仅接受两个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM