问题
I am new to javascript and I cant seam to hide the div's to start with, I can get the divs to switch between each other any help would be great
<script type="text/javascript">
function show(elementId) {
document.getElementById("id1").style.display="none";
document.getElementById("id2").style.display="none";
document.getElementById("id3").style.display="none";
document.getElementById(elementId).style.display="block";
}
</script>
<button type="button" onclick="show('id1');">Button 1</button>
<button type="button" onclick="show('id2');">Button 2</button>
<button type="button" onclick="show('id3');">Button 3</button>
<div id="id1">text 1</div>
<div id="id2">text 2</div>
<div id="id3">text 3</div>
回答1:
You can hide all the divs by adding inline styles:
<script type="text/javascript">
function show(elementId) {
document.getElementById("id1").style.display="none";
document.getElementById("id2").style.display="none";
document.getElementById("id3").style.display="none";
document.getElementById(elementId).style.display="block";
}
</script>
<button type="button" onclick="show('id1');">Button 1</button>
<button type="button" onclick="show('id2');">Button 2</button>
<button type="button" onclick="show('id3');">Button 3</button>
<div id="id1" style="display:none">text 1</div>
<div id="id2" style="display:none">text 2</div>
<div id="id3" style="display:none">text 3</div>
See it working here: http://jsbin.com/suhok/2/
回答2:
<script type="text/javascript">
function show(elementId) {
document.getElementById("id1").style.display="none";
document.getElementById("id2").style.display="none";
document.getElementById("id3").style.display="none";
document.getElementById(elementId).style.display="block";
}
</script>
<style>
div{
display:none;
}
</style>
<button type="button" onclick="show('id1');">Button 1</button>
<button type="button" onclick="show('id2');">Button 2</button>
<button type="button" onclick="show('id3');">Button 3</button>
<div id="id1">text 1</div>
<div id="id2">text 2</div>
<div id="id3">text 3</div>
All the div's will be hidden on the first go, you code will work this way, put buttons and div's under <body> tag and <script> and <style> under <head> tag
here is JSFindle Link
回答3:
I would use both CSS and JavaScript to accomplish this: http://jsfiddle.net/maximgladkov/XaMzB/1/
JavaScript
window.show = function(elementId) {
var elements = document.getElementsByTagName("div");
for (var i = 0; i < elements.length; i++)
elements[i].className = "hidden";
document.getElementById(elementId).className = "";
}
CSS
.hidden {
display: none;
}
HTML
<button type="button" onclick="show('id1');">Button 1</button>
<button type="button" onclick="show('id2');">Button 2</button>
<button type="button" onclick="show('id3');">Button 3</button>
<div id="id1">text 1</div>
<div id="id2" class="hidden">text 2</div>
<div id="id3" class="hidden">text 3</div>
回答4:
You can use pure CSS to hide the divs to begin with:
#id1, #id2 #id3 {
display: none;
}
回答5:
Your code's fine. You just need to put the script
block after everything else.
DEMO
<button type="button" onclick="show('id1');">Button 1</button>
<button type="button" onclick="show('id2');">Button 2</button>
<button type="button" onclick="show('id3');">Button 3</button>
<div id="id1">text 1</div>
<div id="id2">text 2</div>
<div id="id3">text 3</div>
<script type="text/javascript">
function show(elementId) {
document.getElementById("id1").style.display = "none";
document.getElementById("id2").style.display = "none";
document.getElementById("id3").style.display = "none";
document.getElementById(elementId).style.display = "block";
}
</script>
回答6:
<script type="text/javascript">
function hideAll(){
document.getElementById("id1").style.display="none";
document.getElementById("id2").style.display="none";
document.getElementById("id3").style.display="none";
}
function show(elementId) {
hideAll();
document.getElementById(elementId).style.display="block";
}
</script>
<button type="button" onclick="show('id1');">Button 1</button>
<button type="button" onclick="show('id2');">Button 2</button>
<button type="button" onclick="show('id3');">Button 3</button>
<div id="id1">text 1</div>
<div id="id2">text 2</div>
<div id="id3">text 3</div>
<script type="text/javascript">
hideAll();
</script>
jsfiddle: http://jsfiddle.net/y7YKV/
回答7:
Declare show method globally. See the fiddle. I think that's what you needed.
window.show= function(elementId) {
document.getElementById("id1").style.display="none";
document.getElementById("id2").style.display="none";
document.getElementById("id3").style.display="none";
document.getElementById(elementId).style.display="block";
}
JSFiddle
来源:https://stackoverflow.com/questions/21730782/show-hide-divs-with-javascript-on-a-button-press-and-have-all-divs-hidden-fir