admin管理员组文章数量:1323035
I am having some trouble with my webpage. I researched on this topic for 30 minutes straight and still found no direct answer.
If I click button1:
image1 visible
image2 invisible
image3 invisible
If I click button2:
image1 invisible
image2 visible
image3 invisible
If I click button3:
image1 invisible
image2 invisible
image3 visible
Here is my Javascript:
function rock(){
document.getElementById('image1').style.display = 'block';
}
document.getElementById('image2').style.display = 'none';
}
document.getElementById('image3').style.display = 'none';
}
function paper(){
document.getElementById('image1').style.display = 'none';
}
document.getElementById('image2').style.display = 'block';
}
document.getElementById('image3').style.display = 'none';
}
function scissors(){
document.getElementById('image1').style.display = 'none';
}
document.getElementById('image2').style.display = 'none';
}
document.getElementById('image3').style.display = 'block';
}
Here is my HTML:
<button id="rock" onclick="rock()">Rock</button>
<button id="paper" onclick="paper()">Paper</button>
<button id="scissors" onclick="scissors()">Scissors</button>
I am having some trouble with my webpage. I researched on this topic for 30 minutes straight and still found no direct answer.
If I click button1:
image1 visible
image2 invisible
image3 invisible
If I click button2:
image1 invisible
image2 visible
image3 invisible
If I click button3:
image1 invisible
image2 invisible
image3 visible
Here is my Javascript:
function rock(){
document.getElementById('image1').style.display = 'block';
}
document.getElementById('image2').style.display = 'none';
}
document.getElementById('image3').style.display = 'none';
}
function paper(){
document.getElementById('image1').style.display = 'none';
}
document.getElementById('image2').style.display = 'block';
}
document.getElementById('image3').style.display = 'none';
}
function scissors(){
document.getElementById('image1').style.display = 'none';
}
document.getElementById('image2').style.display = 'none';
}
document.getElementById('image3').style.display = 'block';
}
Here is my HTML:
<button id="rock" onclick="rock()">Rock</button>
<button id="paper" onclick="paper()">Paper</button>
<button id="scissors" onclick="scissors()">Scissors</button>
Share
Improve this question
edited Apr 7, 2014 at 21:05
Ms. Speled
asked Apr 7, 2014 at 20:52
Ms. SpeledMs. Speled
351 gold badge1 silver badge8 bronze badges
1
- 1 If you want us to tell you what's wrong with what you've tried, you have to post the code. We're not going to just write it for you. – Barmar Commented Apr 7, 2014 at 20:55
2 Answers
Reset to default 2simply use this to hide it:
document.getElementById("thingid").style.visibility="hidden";
use this to show it:
document.getElementById("thingid").style.visibility="visible";
Very nice tutorial on this page: http://www.w3schools./jsref/prop_style_visibility.asp
2ndary suggestion: you can use jquery to create nice transition effects
- Give id to all buttons
- Give id to all images
- Add event handler to each button
Hide an image like this
document.getElementById("id-of-the-image").style.display = "none";
Show an image like this
document.getElementById("id-of-the-image").style.display = "inline-block";
Your code (with correction)
function rock(){
document.getElementById('image1').style.display = 'block';
document.getElementById('image2').style.display = 'none';
document.getElementById('image3').style.display = 'none';
}
function paper(){
document.getElementById('image1').style.display = 'none';
document.getElementById('image2').style.display = 'block';
document.getElementById('image3').style.display = 'none';
}
function scissors(){
document.getElementById('image1').style.display = 'none';
document.getElementById('image2').style.display = 'none';
document.getElementById('image3').style.display = 'block';
}
An optimized version of your code
function showImage(imageId) {
document.getElementById(imageId).style.display = 'block';
}
function hideImage() {
document.getElementById(imageId).style.display = 'none';
}
function rock(){
showImage('image1');
hideImage('image2');
hideImage('image3');
}
function paper(){
hideImage('image1');
showImage('image2');
hideImage('image3');
}
function scissors(){
hideImage('image1');
hideImage('image2');
showImage('image3');
}
本文标签: javascriptMaking images appear AND other images disappear with the click of a buttonStack Overflow
版权声明:本文标题:javascript - Making images appear AND other images disappear with the click of a button - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742108739a2421144.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论