我想在页面加载时显示id为“houseImages”的div,同时隐藏div“landImages”,“renovImages”,“UpcomingImages”和“voteForNext”。单击“Landscaping”后,我想显示div“landImages”并隐藏div“houseImages”。显然,我希望这个功能继续通过列表,当我点击“绘画”时div会重新出现。我如何使用JavaScript?
这是我到目前为止所做的:
<Div id="mainContent">
<h2> Our House </h2>
<div id="columnLeft">
<ul>
<li id="leftColumnPainting">
<a href="#houseImages">Painting</a>
</li>
<li id="leftColumnLandscaping">
<a href="landscaping.html">Landscaping</a>
</li>
<li id="leftColumnRenovations">
<a href="renovations.html">Renovations</a>
</li>
<li id="leftColumnUpcoming">
<a href="upcoming.html">Upcoming</a>
</li>
<li id="leftColumnNext">
<a href="whatsnext.html">Vote for <br>Next!</br></a>
</li>
</ul>
</div>
<div id="columnRight">
<div id="title">
<p> Over the course of a couple years we have done a number of projects around the house. Click on a link to your left to see the improvements! Don't forget to vote for what's next!
</p>
</div>
<div id="houseImages">
<ul>
<li>
<a href="Painting.html"><img src="./img/house1.jpg" /></a>
</li>
<li>
<a href="Painting.html"><img src="./img/house2.jpg" /></a>
</li>
<li>
<a href="Painting.html"><img src="./img/house3.jpg" /></a>
</li>
<li>
<a href="Painting.html"><img src="./img/house4.jpg" /></a>
</li>
</ul>
</div>
<div id="landImages">
<ul>
<li>
<a href="Painting.html"><img src="./img/land1.jpg" /></a>
</li>
<li>
<a href="Painting.html"><img src="./img/land2.jpg" /></a>
</li>
<li>
<a href="Painting.html"><img src="./img/land3.jpg" /></a>
</li>
<li>
<a href="Painting.html"><img src="./img/land4.jpg" /></a>
</li>
</ul>
</div>
<div id="renovationImages">
<ul>
<li>
<a href="Painting.html"><img src="./img/renovation1.jpg" /></a>
</li>
<li>
<a href="Painting.html"><img src="./img/renovation2.jpg" /></a>
</li>
<li>
<a href="Painting.html"><img src="./img/renovation3.jpg" /></a>
</li>
<li>
<a href="Painting.html"><img src="./img/renovation4.jpg" /></a>
</li>
</ul>
</div>
<div id="upcomingImages">
<ul>
<li>
<a href="Painting.html"><img src="./img/upcoming1.jpg" /></a>
</li>
<li>
<a href="Painting.html"><img src="./img/upcoming2.jpg" /></a>
</li>
<li>
<a href="Painting.html"><img src="./img/upcoming3.jpg" /></a>
</li>
<li>
<a href="Painting.html"><img src="./img/renovation4.jpg" /></a>
</li>
</ul>
</div>
<div id="voteForNext">
<p> enter in voting form here</p>
</div>
</div>
</Div>
答案 0 :(得分:0)
我建议您将<a href="">
更改为您要显示的div的ID。然后你可以做类似的事情:
$("#columnleft li a").click(function(e) {
e.preventDefault();
$("columnright div").hide();
$("#"+$(this).attr("href")).show();
});
答案 1 :(得分:0)
我个人喜欢隐藏 - &gt; fadeIn效果组合。如果你有一个2个按钮,一个2个div,你可以切换哪个显示如下:
HTML:
<div id="a">div 1</div>
<div id="b">div 2</div>
<button id="c">show div 1</button>
<button id="d">show div 2</button>
CSS:
div{
width: 200px;
height: 200px;
border: solid 1px;
display: none;
}
JS:
$('#c').click(function() {
$('#b').hide();
$('#a').fadeIn();
});
$('#d').click(function() {
$('#a').hide();
$('#b').fadeIn();
});
参见JSfiddle:http://jsfiddle.net/HSNLN/
您可以将此方法附加到可以使用jQuery捕获的任何事件,以及显示/隐藏和div。