【问题标题】:faq toggle div show/hide + change link text常见问题切换 div 显示/隐藏 + 更改链接文本
【发布时间】:2013-08-15 07:58:14
【问题描述】:

我观看/阅读/搜索了许多教程,但没有运气。 我坚持this tutorial

我正在尝试使用 JS(可能仅限 CSS)不针对应用 togle 手风琴效果到 div。

有两种显示内容的选项:

  1. 一个问题打开/另一个关闭
  2. 每个问题都可以单独打开/关闭

我不介意,你选择哪个选项来解决这个问题。

无法更改任何 ID 元素的属性(例如 id="div1"、id="div2"、...)

我的目标是制作如图所示的东西:

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>   

我的尝试 JS:

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

JsFiddle:

JsFiddle example

解决方案:

(将&lt;button&gt;show&lt;/button&gt;放在boxTitle div之后)

代码:

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

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

});

【问题讨论】:

    标签: javascript css toggle show-hide


    【解决方案1】:
    $(this).parent().next('.toggle').slideToggle();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多