【发布时间】:2014-02-08 02:40:02
【问题描述】:
我正在尝试创建在单击时显示其他 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”,按钮根本不起作用。我做错了什么?
【问题讨论】:
标签: javascript css html