簡體   English   中英

Bootstrap手風琴在標題面板中顯示/隱藏文本

[英]Bootstrap accordion with show / hide text in header panel

如果應該隱藏面板打開的“扇區和問題”標簽,並且如果面板已折疊,則應該顯示“扇區和問題”。

這是我的js文件

          $(window).load(function () {
            var parent = $('.panel-group .panel-heading');
            parent.each(function (i, el) {
                //el is element used for "{var parent}" class to execute value one by one 
                var el = $(el);
                var collapsed = el.find('.panel-title a').hasClass('collapsed');

                if (collapsed == true) {
                    el.find(".show-hide-si").show('slow');
                } else {
                    el.find(".show-hide-si").hide('slow');
                }

                /**
                 * this will run on click to show and hide for sector&issues
                 * this is tricky,if you click and class is collapsed 
                 */
                el.find('.panel-title a').click(function () {
                    var collapsed = $(this).hasClass('collapsed');
                    if (collapsed == false) {
                        el.find(".show-hide-si").show('slow');
                    } else {
                        el.find(".show-hide-si").hide('slow');
                    }
                });

            });
        });

這是可行的,但是如果panel-p1是打開的,並且我單擊panel-p2,則兩個面板文本都會被隱藏。

我的數據是動態的,因此使用這樣的類<div id="collapse_<?php print $key; ?>"而不是crashOne,Two,Three。

在HTML中

<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
<div class="panel panel-default" id="panel<?php print $key; ?>">
    <div class="panel-heading" role="tab" id="heading_<?php print $key; ?>">
        <h4 class="panel-title">
            <a <?php print $clsa; ?> role="button" data-toggle="collapse" data-parent="#accordion" href="#collapse_<?php print $key; ?>" aria-expanded="true" aria-controls="collapse_<?php print $key; ?>">
                Program : <?php print $ptitle; ?>
            </a>                
        </h4>
        <div class="show-hide-si">
            <div class="sector">Sectors : <?php print $sectors_of_interes; ?></div>
            <div class="issues">Issues : <?php print $pro_problem; ?></div>
        </div>
    </div>

    <div id="collapse_<?php print $key; ?>" class="panel-collapse <?php print $clsin; ?>" role="tabpanel" aria-labelledby="heading_<?php print $key; ?>">
        <div class="panel-body">

另一個問題是,如果我在面板P2上單擊,則它是打開的,然后我單擊P1(P1關閉),P1打開,但P2沒有關閉,兩個都打開。

看這個例子

 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Example of Bootstrap 3 Accordion</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> <style type="text/css"> .bs-example{ margin: 20px; } </style> </head> <body> <div class="bs-example"> <div class="panel-group" id="accordion"> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">1. What is HTML?</a> </h4> </div> <div id="collapseOne" class="panel-collapse collapse in"> <div class="panel-body"> <p>HTML stands for HyperText Markup Language. HTML is the main markup language for describing the structure of Web pages. <a href="http://www.tutorialrepublic.com/html-tutorial/" target="_blank">Learn more.</a></p> </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-parent="#accordion" href="#collapseTwo">2. What is Bootstrap?</a> </h4> </div> <div id="collapseTwo" class="panel-collapse collapse"> <div class="panel-body"> <p>Bootstrap is a powerful front-end framework for faster and easier web development. It is a collection of CSS and HTML conventions. <a href="http://www.tutorialrepublic.com/twitter-bootstrap-tutorial/" target="_blank">Learn more.</a></p> </div> </div> </div> <div class="panel panel-default"> <div class="panel-heading"> <h4 class="panel-title"> <a data-toggle="collapse" data-parent="#accordion" href="#collapseThree">3. What is CSS?</a> </h4> </div> <div id="collapseThree" class="panel-collapse collapse"> <div class="panel-body"> <p>CSS stands for Cascading Style Sheet. CSS allows you to specify various style properties for a given HTML element such as colors, backgrounds, fonts etc. <a href="http://www.tutorialrepublic.com/css-tutorial/" target="_blank">Learn more.</a></p> </div> </div> </div> </div> </div> </body> </html> 

在引導程序中,您可以像這樣調用事件方法

$('#accordian').on('hidden', function () {
  // do something…
})

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM