简体   繁体   English

根据案件陈述动态打开手风琴

[英]dynamically open accordion based on case statment

I am trying to select what accordion has the .active class set based on the results of a switch/case statement. 我试图根据switch / case语句的结果选择手风琴具有.active类设置的内容。

I currently have a switch/case statement set to hide tabs depending on the statement. 我目前有一个switch / case语句设置隐藏选项卡,具体取决于语句。

<script>
        $(function () {
            $("#accordion-test").accordion({
                collapsible: true,
                active: '#accordion-test .active'

            });
        });
    </script>

<div id="accordion-test">
    <h3 class="content1 one">Content Title</h3>

    <div align="left">
        <p>Content for 1 Goes Here</p>
    </div>
    <h3 class="two">Content Title</h3>

    <div align="left">
        <p>Content for 2 Goes Here</p>
    </div>
    <h3 class="active three">Content Title</h3>

    <div align="left">
        <p>Content for 3 Goes Here</p>
    </div>
</div>

The statement 该声明

the session varible is set to one, two or three 会话变量设置为一,二或三

      <?php
            switch ($_SESSION['session']) {

                    case "one":
                        ?>
                        <style type="text/css">
                            .two,.three {
                                display: none !important;
                            }
                        </style>
                        <?php
    break;
                    case "one":
                        ?>
                        <style type="text/css">
                            .two,.three {
                                display: none !important;
                            }
                        </style>
                        <?php
break;
                    case "two":
                        ?>
                        <style type="text/css">
                            .one,.three {
                                display: none !important;
                            }
                        </style>
                        <?php
    case "three":
                        ?>
                        <style type="text/css">
                            .one,.two{
                                display: none !important;
                            }
                        </style>
}
                        <?php

with this statement I can show or hide accordion tabs depending on the variable present one , two or three . 使用此语句,我可以显示或隐藏折叠选项卡,具体取决于存在onetwothree变量。

I would now like to make the displayed accordion tab active, 我现在想让显示的手风琴标签处于活动状态,

for example 例如

case "one":
                        ?>
                        <style type="text/css">
                            .two,.three {
                                display: none !important;
                            }

HAVE ACTIVE STATE APPLIED TO ACCORDION TAB ONE
                        </style>
                        <?php


   <?php
    case "three":
                        ?>
                        <style type="text/css">
                            .one,.two{
                                display: none !important;
                            }
HAVE ACTIVE STATE APPLIED TO ACCORDION TAB THREE
                        </style>
                        <?php

You are trying to bypass using accordion to make its selections and overriding it with styles. 您试图绕过使用accordion进行选择并用样式覆盖它。 That is fighting against the way it should work (using accordion). 这是与它应该工作的方式作斗争(使用手风琴)。 Instead of generating styles dynamically, just tell the accordion which tab to open at startup. 而不是动态生成样式,只需告诉手风琴哪个选项卡在启动时打开。

Here is an example that avoids any server-side generation of styles: http://jsfiddle.net/TrueBlueAussie/d6mSA/1187/ 这是一个避免任何服务器端样式生成的示例: http//jsfiddle.net/TrueBlueAussie/d6mSA/1187/

$(function() {
  var $accordion = $("#accordion");
  $accordion.accordion({
    autoHeight: true,
    collapsible: true
  });
  // Open the selected tab from the element
  $accordion.accordion('activate', $accordion.data("selection"));
});

It just picks up a data- attribute with the current selection number in it and tells the accordion to open that one: 它只是获取一个带有当前选择号的data-属性,并告诉手风琴打开那个:

 <div id="accordion" data-selection="2">

Or even simpler, just set the active value from the data- : 或者甚至更简单,只需从data-设置活动值:

$(function() {
  var $accordion = $("#accordion");
  $accordion.accordion({
    autoHeight: true,
    collapsible: true,
    active:  $accordion.data("selection")
  });
});

JSFiddle: http://jsfiddle.net/TrueBlueAussie/d6mSA/1188/ JSFiddle: http//jsfiddle.net/TrueBlueAussie/d6mSA/1188/

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

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