在我的一个项目中,我制作了3个图库,我想把它们都放在同一页上的同一位置,但不是同时显示。为了实现这个目标,我选择创建了3个按钮。例如,当我点击第一个按钮时,第一个图库应该出现(两个图库最初都是display:none),然后当我点击第二个按钮时,第二个图库应该出现,之前显示的那个应该消失,对于每个图库都是如此。我制作了一个简化版的页面,以便更容易地思考。
总的来说,我的问题是我不太知道如何将函数应用到数组中的所有元素,除了一个元素。
以下是代码:
总的来说,我的问题是我不太知道如何将函数应用到数组中的所有元素,除了一个元素。
以下是代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Galleries</title>
<link rel="stylesheet" type="text/css" href="gs.css">
<style type="text/css">
body{
background-color:royalblue;
}
header{
text-align: center;
}
article{
width:95%;
margin:auto 2.5% auto 2.5%;
height:850px;
background-color:tomato;
display:none;
}
</style>
</head>
<body>
<header>
<button>Third Gallery</button>
<button>Second Gallery</button>
<button>Third Gallery</button>
</header>
<section>
<article>
<h1>This is the first gallery</h1>
</article>
<article>
<h1>This is the second gallery</h1>
</article>
<article>
<h1>This is the third gallery</h1>
</article>
</section>
<script type="text/javascript">
var button=document.getElementsByTagName('button');
var gallery=document.getElementsByTagName('article');
for(var i=0; i<button.length; i++){
(function(index){
button[index].onclick=function(){
gallery[index].style.display="block";
}
}(i));
}
</script>
</body>
</html>