简体   繁体   English

切换显示/隐藏HTML内容

[英]Toggle show/hide html content

I'm trying to create a script/flow. 我正在尝试创建一个脚本/流程。 I'd like to use jQuery to swap html elements (12 total divs) and stay in one, centered and responsive section element. 我想使用jQuery交换html元素(12个div)并保持在一个,居中和响应部分元素。 The first div is visible and the rest hidden. 第一个div是可见的,其余的隐藏。 In each div, and not in a static button list, there would be links that let you hide the current div, and unhide another div based on its id. 在每个div中,而不是在静态按钮列表中,会有链接让您隐藏当前div,并根据其ID取消隐藏另一个div。

JQuery Toggle seems to be the most easy, but I'm not figuring out how to toggle more than 2 elements. JQuery Toggle似乎是最简单的,但我不知道如何切换超过2个元素。 I'm open to any solutions, however. 不过,我对任何解决方案持开放态度。

<section>
<div id="a">
    <p>content here</p>
        <div class="links">
            <a href="#" id="b-link">b</a> <a href="#" id="c-link">c</a>
        </div>
</div>

<div class="hidden" id="b">
    <p>content here</p>
        <div class="links">
            <a href="#" id="a-link">a</a> <a href="#" id="c-link">c</a> 
<a href="#" id="d-link">d</a>
        </div>
</div>

<div class="hidden" id="c">
    <p>content here</p>
        <div class="links">
            <a href="#" id="a-link">a</a> <a href="#" id="b-link">b</a>
        </div>
</div>

<div class="hidden" id="d">
    <p>content here</p>
        <div class="links">
            <a href="#" id="a-link">c</a> <a href="#" id="b-link">a</a>
        </div>
</div>
</section>

Maybe this link can help you to choose the selector for all elements except the selected and then apply the jquery toggle 也许此链接可以帮助您为除选定项之外的所有元素选择选择器,然后应用jquery切换

How do I use jQuery to select all children except a select element 如何使用jQuery选择除select元素之外的所有子元素

This works as you have requested, I've made a few changes to the code. 这可以按照您的要求工作,我对代码进行了一些更改。

The buttons have a custom attribute for which corresponds to the id of the panel which they display. 这些按钮具有自定义属性for该属性对应于它们显示的面板的id This then toggles the class .active which shows the panel via CSS . 然后切换类.active ,通过CSS显示面板。 Panels are by default hidden until they are 'active'. 默认情况下,面板是隐藏的,直到它们处于“活动”状态。

The jquery is pretty straight forward, attaching a click event to any button with the class .panel-switch . jquery非常简单,将click事件附加到带有.panel-switch类的任何按钮。 It is fully commented below. 它在下面完全评论。 My preference is to use CSS but instead of adding a class you could simply .toggle() the DOM elements using the same selection method. 我的偏好是使用CSS但不是添加类,而是使用相同的选择方法简单地.toggle() DOM元素。

Let me know if this wasn't what you were hoping for. 如果这不是你所希望的,请告诉我。


Demo 演示

 // Add click event to the buttons with class panel-switch $(".panel-switch").click(function() { // Hide current active panel $(".panel.active").removeClass("active"); // Activate chosen panel $(".panel#" + $(this).attr("for")).addClass("active"); }); 
 .panel { display: none; } .panel.active { display: inherit; } 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <section> <div class="panel active" id="a"> <p>Panel A</p> <div class="links"> <button class="panel-switch" for="b">b</button> <button class="panel-switch" for="c">c</button> </div> </div> <div class="panel" id="b"> <p>Panel B</p> <div class="links"> <button class="panel-switch" for="a">a</button> <button class="panel-switch" for="d">d</button> </div> </div> <div class="panel" id="c"> <p>Panel C</p> <div class="links"> <button class="panel-switch" for="a">a</button> <button class="panel-switch" for="b">b</button> </div> </div> <div class="panel" id="d"> <p>Panel D</p> <div class="links"> <button class="panel-switch" for="c">c</button> <button class="panel-switch" for="a">a</button> </div> </div> </section> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM