【发布时间】:2011-04-15 01:35:11
【问题描述】:
我是 Javascript 的新手。
我正在尝试使用显示/隐藏功能做一些事情。
html:
<html>
<head>
<title> New Document </title>
<style>
#button01 {
width:100px;
height:50px;
margin:10px;
padding:6px 0 0 0;
background-color:#f0f0f0;
}
#button01:hover {
background-color:#ffcccc;
}
#button01 a {
display:block;
width:40px;
height:40px;
margin:auto;
background:url("button01.png")
}
#button01 a:hover {
width:40px;
height:40px;
background:url("button01-hover.png")
}
#hidden01 {
display:none;
width:300px;
height:200px;
margin:0 0 10px 0;
border:4px solid #ffcccc;
}
#button02 {
width:100px;
height:50px;
margin:10px;
padding:6px 0 0 0;
background-color:#f0f0f0;
}
#button02:hover {
background-color:#cccccc;
}
#button02 a {
display:block;
width:40px;
height:40px;
margin:auto;
background:url("button02.png")
}
#button02 a:hover {
width:40px;
height:40px;
background:url("button02-hover.png")
}
#hidden02 {
display:none;
width:300px;
height:200px;
margin:0 0 10px 0;
border:4px solid #cccccc;
}
</style>
</head>
<body>
<div style="width:300px;">
<div id="button01"><a href="#" onclick="toggle(0);return false"></a></div>
<div id="button02"><a href="#" onclick="toggle(1);return false"></a></div>
</div>
<div id="hidden01"> </div>
<div id="hidden02"> </div>
</body>
</html>
脚本:
function toggle(offset){
var i, x;
var stuff = Array('hidden01', 'hidden02'); //put all the id's of the divs here in order
for (i = 0; i < stuff.length; i++){ //hide all the divs
x = document.getElementById(stuff[i]);
x.style.display = "none";
}
// now make the target div visible
x = document.getElementById(stuff[offset]);
x.style.display = "block";
window.onload = function(){toggle(0);}
}
这行得通,但我想解决两件事:
1- 如果我点击它的相应按钮,则关闭/隐藏隐藏的 div;
2- 单击按钮后,修复悬停按钮图像。如果再次点击 unfix;
我已经尝试了几乎所有发布的脚本,但找不到解决方案。我不想同时打开 div。 如果打开一个,关闭其他。
【问题讨论】:
-
更少的 JavaScript,更多的 jQuery。
标签: javascript jquery