【问题标题】:How can I attach an event to the selection of Bootstrap collapsing panel?如何将事件附加到 Bootstrap 折叠面板的选择?
【发布时间】:2016-09-07 14:38:10
【问题描述】:

在下面的代码中,有一个没有列表的 Bootstrap 可折叠面板,因此它不会展开或收缩。如何将事件附加到此面板的选择中?

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="panel-group">
            <div class="panel panel-default" id="leftsidemenu">


<div class="panel-heading" id="aboutcollapsepanel">
                    <h4 class="panel-title">
                        <a data-toggle="collapse" href="#collapseabout"  data-target="tabsabout" >About</a>
                    </h4>
                </div>
                <div id="collapseabout" class="panel-collapse collapse">

                </div>



	     </div>
</div>

我的非工作尝试如下:

$("#aboutcollapsepanel").on('show.bs.collapse', function () {
    alert("collapse panel activated");
});

$("#aboutcollapsepanel").on('hide.bs.collapse', function () {
    alert("collapse panel deactivated");
});

如何获取面板的数据目标值,在本例中为tabsabout,它是一个 jQueryUI 选项卡小部件。我的目标是获取此 id 并将其传递给使其可见的 javascript 函数,并且该函数具有签名 function performListItemAction(item) 其中item 将是我单击面板中数据目标的值。

谢谢。

【问题讨论】:

    标签: javascript jquery html twitter-bootstrap jquery-ui


    【解决方案1】:

    为什么不添加 onClick?

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <script>
    function performListItemAction(item) {
    alert(item);
    }
    </script>
    <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <div class="panel-group">
                <div class="panel panel-default" id="leftsidemenu">
    
    
    <div class="panel-heading" id="aboutcollapsepanel">
                        <h4 class="panel-title">
                            <a data-toggle="collapse" href="#collapseabout"  data-target="tabsabout" onClick="performListItemAction('tabsabout')">About</a>
                        </h4>
                    </div>
                    <div id="collapseabout" class="panel-collapse collapse">
    
                    </div>
    
    
    
    	     </div>
    </div>

    【讨论】:

      【解决方案2】:

      我不确定我是否理解您的尝试。你只需要定位#collapseabout。这既是 Bootstrap 可折叠元素,也是数据目标。然后您可以在显示处理程序中调用 performListItemAction()。

      <div class="panel-group">
          <div class="panel panel-default" id="leftsidemenu">
              <div class="panel-heading" id="aboutcollapsepanel" data-toggle="collapse" data-target="#collapseabout">
                  <h4 class="panel-title">
                      About
                  </h4>
              </div>
              <div id="collapseabout" class="panel-collapse collapse">
                      about content here..
              </div>
          </div>
      </div>
      

      http://www.codeply.com/go/IvWxFtgoJo

      【讨论】:

      • 我正在尝试通过 performListItemAction() 方法让一个 jQueryUI 选项卡面板(id 为 tabsabout )出现在屏幕上。我认为我的困惑是由于我对 data-target 属性的理解引起的,因为我在您的回复中看到它的值是指单击主面板标题时展开的子菜单。但是,在我的情况下,没有子菜单可以展开,我想将data-target 中指定的id 发送到performListItemAction() 方法,以便可以显示jQueryUI 选项卡式面板。
      • 也许你可以用你的jQueryUI代码发布一个更完整的例子?单独的 Bootstrap 折叠组件可用于切换(显示/隐藏)面板我不明白 jQueryUI 是如何参与的。
      • 基本上,我试图执行一种方法,当单击 Bootstrap 可折叠时,该方法将在与可折叠面板的内容区域不同的页面区域上显示 jQueryUI 选项卡式小部件。我现在已经收到答案了,感谢您的帮助。
      猜你喜欢
      • 2017-06-14
      • 1970-01-01
      • 2016-02-28
      • 1970-01-01
      • 2017-06-03
      • 1970-01-01
      • 2015-11-06
      • 1970-01-01
      • 2016-08-23
      相关资源
      最近更新 更多