我试图创建按钮(这是指div),点击后,显示其他的div。例如,Button01显示Div01,Button02显示Div02。一次只能显示一个Div。我有这部分的工作,但我碰到的一个问题,即,在页面加载后,点击功能不直到按钮的div被点击一次(这似乎什么都不做)工作。在第一次点击之后,按钮div可以正常工作。为什么会发生这种情况,我怎样才能让点击功能马上工作?页面加载后,按钮只能当它被点击了第二次
HTML
<div id="showOne" class="button" onClick="showOne()">Show One</div>
<div id="showTwo" class="button" onClick="showTwo()">Show Two</div>
<div id="One"></div>
<div id="Two"></div>
CSS
.button {
width:70px;
height:81px;
background-color:#ccc;
float:left;
text-align:center;
}
#One {
width:70px;
height:81px;
background-color:blue;
float:left;
display:none;
}
#Two {
width:70px;
height:81px;
background-color:red;
float:left;
display:none;
}
的Javascript
function showOne(){
if (document.getElementById('Two').style.display != "none") {
document.getElementById('Two').style.display = "none";
};
if (document.getElementById('One').style.display == "none") {
document.getElementById('One').style.display = "block";
} else {
document.getElementById('One').style.display = "none";
};
};
function showTwo(){
if (document.getElementById('One').style.display != "none") {
document.getElementById('One').style.display = "none";
};
if (document.getElementById('Two').style.display == "none") {
document.getElementById('Two').style.display = "block";
} else {
document.getElementById('Two').style.display = "none";
};
};
演示: http://jsfiddle.net/7BtZn/1/
正如你所看到的使用“不循环 - 在”,一个按钮被点击一次后第一按钮才有效(这是发生了什么事我的实际页)。值得注意的是,使用“onLoad”,按钮根本不起作用。我究竟做错了什么?
谢谢!完美的作品:D – user3208943