简体   繁体   English

常见问题切换div显示/隐藏+更改链接文本

[英]faq toggle div show/hide + change link text

I watched/read/search many tutorials, but with no luck. 我看过/读过/搜索过很多教程,但是没有运气。 I stick with this tutorial . 我坚持使用本教程

I am trying to apply toogle acordion effect with JS (possibly CSS only) without targeting to divs. 我正在尝试对JS(可能仅CSS)应用toogle acordion效果, 而不定位到div。

There are 2 options to show content: 有两种显示内容的选项:

  1. One question open / other closed 一个问题未解决/其他问题未解决
  2. Each question can be open / close separatly 每个问题可以分开打开/关闭

I don't mind, which option you choose to solve this problem. 我不介意,您选择了哪个选项来解决此问题。

I can't change any atribute to ID elements (eg id="div1", id="div2", ...) 无法更改 ID元素的任何属性 (例如id =“ div1”,id =“ div2”,...)

My goal is to make something like in picture: 我的目标是制作类似图片的图片: 预习

HTML: HTML:

    <div class="widget widgetFaq clearfix">

    <div class="widgetTitle"> 
        <h2>FAQ</h2> 
    </div>   

    <div class="widgetContent clearfix">   

        <div class="box boxFaq clearfix">                  

            <div class="boxTitle">
                <h3>Question one?</h3>
                <button> show </button>
            </div>

            <div class="boxContent clearfix toggle">                     
                <p>Answer to the question. Answer to the question. Answer to the question. </p>
            </div>

        </div>  

        <div class="box boxFaq clearfix">                  

            <div class="boxTitle">
                <h3>Question second?</h3>
                <button> show </button>
            </div>

            <div class="boxContent clearfix toggle">                     
                <p>Answer to the question. Answer to the question. Answer to the question. </p>
            </div>

        </div>  

    </div>   

</div>   

My try JS: 我尝试JS:

   $(document).ready(function(){ 
    $('button').click(function() {
        $(this).next('.toggle').slideToggle();
    });
});

JsFiddle: JsFiddle:

JsFiddle example JsFiddle示例

SOLUTION: 解:

( put <button>show</button> after boxTitle div ) (将<button>show</button> boxTitle div之后)

Code: 码:

$(document).ready(function() {
$('button').click(function() {
    $(this).next('.toggle').slideToggle();

    if ($(this).text() === '+') {
        $(this).html('-');
    }
    else {
        $(this).html('+');
    }
});

}); });

$(this).parent().next('.toggle').slideToggle();

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

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