JavaScript/Jquery - 隐藏显示的 div,显示另一个,然后重复

标签 javascript jquery html hide show

我想在页面加载时显示 id 为“houseImages”的 div,同时隐藏 div“landImages”、“renovationImages”、“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>

最佳答案

我建议更改您的 <a href="">到您想要显示的 div 的 ID。然后你可以这样做:


$("#columnleft li a").click(function(e) {
   e.preventDefault();
   $("columnright div").hide();
   $("#"+$(this).attr("href")).show();
});

关于JavaScript/Jquery - 隐藏显示的 div,显示另一个,然后重复,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11055466/

相关文章:

javascript - 列表项事件悬停效果

javascript - 用 Javascript 突出显示单词,我错过了什么?

javascript - js中如何判断object中是否存在某个特定的key

javascript - 根据数据点强制面积图的 X 轴刻度线

javascript - 如何在执行后杀死所有函数?

javascript - jQuery post() JSP 返回集合

javascript - 将 jQuery 自动完成数据附加到 textarea 内容而不是覆盖它

javascript - 不在页面上的元素的 jQuery 选择器

html - Z-Index 不适用于位置为 :relative when its parent is position:absolute 的元素

javascript - 从 HTML 页面运行 Javascript 游戏