Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
253 views
in Technique[技术] by (71.8m points)

javascript - Open a specific accordion panel with an external anchor link

I have a bootstrap accordion setup and working fine.

I need to have links on external pages which will:

A) Take you to the specified panel within the accordion

B) Open the specified panel

The link structure needs to be in this format (example):

Page.html#collapseTwo
(the 'collapseTwo' will change, of course, depending on the panel)

The HTML:

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container" 
    id="page_container">
    <div id="accordion_search_bar_container">
        <input type="search" 
            id="accordion_search_bar" 
            placeholder="Search"/>
    </div>
    <div class="panel-group" 
        id="accordion" 
        role="tablist" 
        aria-multiselectable="true">
        <div class="panel panel-success" 
            id="collapseOne_container">
            <div class="panel-heading" 
                role="tab" 
                id="headingOne">
                <h4 class="panel-title">
                    <a role="button" 
                        data-toggle="collapse" 
                        data-parent="#accordion" 
                        href="#collapseOne" 
                        aria-expanded="true" 
                        aria-controls="collapseOne">
                    One
                    </a>
                </h4>
            </div>
            <div id="collapseOne" 
                class="panel-collapse collapse in" 
                role="tabpanel" 
                aria-labelledby="headingOne">
                <div class="panel-body">
                    <p>Pellentesque convallis dolor</p>
                    <p>Enim at tincidunt magna dapibus vitae</p>
                </div>
            </div>
        </div>
        <div class="panel panel-primary" 
            id="collapseTwo_Container">
            <div class="panel-heading" 
                role="tab" 
                id="headingTwo">
                <h4 class="panel-title">
                    <a class="collapsed" 
                        role="button" 
                        data-toggle="collapse" 
                        data-parent="#accordion" 
                        href="#collapseTwo" 
                        aria-expanded="false" 
                        aria-controls="collapseTwo">
                    Two
                    </a>
                </h4>
            </div>
            <div id="collapseTwo" 
                class="panel-collapse collapse in" 
                role="tabpanel" 
                aria-labelledby="headingTwo">
                <div class="panel-body">
                    <p>Vestibulum in laoreet nisi</p>
                    <p>Sit amet placerat massa</p>
                </div>
            </div>
        </div>
        <div class="panel panel-danger" 
            id="collapseThree_Container">
            <div class="panel-heading" 
                role="tab" 
                id="headingThree">
                <h4 class="panel-title">
                    <a class="collapsed" 
                        role="button" 
                        data-toggle="collapse" 
                        data-parent="#accordion" 
                        href="#collapseThree" 
                        aria-expanded="false" 
                        aria-controls="collapseThree">
                    Three
                    </a>
                </h4>
            </div>
            <div id="collapseThree" 
                class="panel-collapse collapse in" 
                role="tabpanel" 
                aria-labelledby="headingThree">
                <div class="panel-body">
                    <p>Curabitur sem eros tempor sit</p>
                    <p>Amet nunc eget, gravida mollis</p>
                </div>
            </div>
        </div>
    </div>
</div>

The JS:

  (function() {
    var searchTerm, panelContainerId;
    // Create a new contains that is case insensitive
    $.expr[':'].containsCaseInsensitive = function(n, i, m) {
        return jQuery(n).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
    };

    $('#accordion_search_bar').on('change keyup paste click', function() {
        searchTerm = $(this).val();
        $('#accordion > .panel').each(function() {
            panelContainerId = '#' + $(this).attr('id');
            $(panelContainerId + ':not(:containsCaseInsensitive(' + searchTerm + '))').hide();
            $(panelContainerId + ':containsCaseInsensitive(' + searchTerm + ')').show();
        });
    });
}());
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use url parameters and do it.

Sample links:
https://output.jsbin.com/talojodupa/1?panel=collapseOne
https://output.jsbin.com/talojodupa/1?panel=collapseTwo

jsbin link: https://jsbin.com/talojodupa/1/edit?html,js,output

(function() {
    var searchTerm, panelContainerId;
    // Create a new contains that is case insensitive
    $.expr[':'].containsCaseInsensitive = function(n, i, m) {
        return jQuery(n).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
    };

    $('#accordion_search_bar').on('change keyup paste click', function() {
        searchTerm = $(this).val();
        $('#accordion > .panel').each(function() {
            panelContainerId = '#' + $(this).attr('id');
            $(panelContainerId + ':not(:containsCaseInsensitive(' + searchTerm + '))').hide();
            $(panelContainerId + ':containsCaseInsensitive(' + searchTerm + ')').show();
        });
    });
  
  /* Function to get url parameter based on param name */
  function getUrlParameter(key) {
    key = key.replace(/[[]/, "\[").replace(/[]]/, "\]");
    var regex = new RegExp("[\?&]" + key + "=([^&#]*)");
    var results = regex.exec(window.location.href);
    if (results == null)
      return "";
    else
      return unescape(results[1]).replace(/<script.*>.*</script>/g, "");
  }
  
  var panelId = getUrlParameter('panel');
  if(panelId){
    var $panel = $('#'+panelId);
    $panel.addClass('in');

    $('html,body').animate({
       scrollTop: $panel.offset().top},
    'slow');
  }
  
}());

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...