javascript - Bootstrap Accordion ,在标题面板中显示/隐藏文本

标签 javascript jquery twitter-bootstrap twitter-bootstrap-3 jquery-ui-accordion

如果面板打开,则“扇区”和“问题”标签应隐藏,如果面板折叠,“扇区”和“问题”标签应可见。

这是我的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; ?>"而不是塌陷一、二、三。

在 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>                                		                                		

在 Bootstrap 中,您可以像这样调用事件方法

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

关于javascript - Bootstrap Accordion ,在标题面板中显示/隐藏文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34085540/

相关文章:

JavaScript RegExp 不能使用两次?

javascript - cytoscape.js cose布局节点重叠

jquery - CSS z-index 不适用于 iPad 上的 bootstrap popover

javascript - 使用 jquery 验证多个输入中的 1 个输入

javascript - 显示函数上的 Bootstrap 模式不传递值

javascript - addClass 活跃到 Bootstrap 导航

javascript - 如何使用 JavaScript 打开新窗口?

javascript - Laravel 5.6 - 选中复选框时增加数字

jquery - 使用 jquery.validate.js 和 jquery.form.js 进行 Ajax 表单提交?

javascript - 为页面加载时不存在的内容设置动画