简体   繁体   English

Bootstrap 3 从 URL 扩展手风琴

[英]Bootstrap 3 expand accordion from URL

Using Bootstrap 3, I'm trying to use sub-navigation anchor links (ie, index.php#wnsh) to expand a specified accordion and anchor down the page to the content.使用 Bootstrap 3,我尝试使用子导航锚链接(即 index.php#wnsh)来展开指定的手风琴并将页面向下锚定到内容。 I've tried searching for examples but with little luck, likely because my accordion structure is different from the given BS3 example.我尝试搜索示例,但运气不佳,可能是因为我的手风琴结构与给定的 BS3 示例不同。 Here's my HTML:这是我的 HTML:

UPDATE:更新:

Made a few updates to the code, but it still isn't opening the accordion specified by the hash.对代码进行了一些更新,但它仍然没有打开哈希指定的手风琴。 Any further thoughts?还有什么想法吗?

            <div id="accordion" class="accordion-group">                
                <div class="panel">
                    <h4 id="cs" class="accordion-title"><a data-toggle="collapse" data-parent="#accordion" data-target="#cs_c">Child Survival: Boosting Immunity and Managing Diarrhoea</a></h4>
                    <div id="cs_c" class="accordion-collapse collapse in">
                        <p>...</p>
                    </div>

                    <h4 id="chgd" class="accordion-title"><a data-toggle="collapse" data-parent="#accordion" data-target="#chgd_c">Child Health, Growth and Development: Preventing Mental Impairment with Iodine and Iron</a></h4>
                    <div id="chgd_c" class="accordion-collapse collapse">
                        <p>...</p>
                    </div>

                    <h4 id="wmnh" class="accordion-title"><a data-toggle="collapse" data-parent="#accordion" data-target="#wmnh_c">Women’s and Newborn Survival and Health: Iron Supplementation and Food Fortification</a></h4>
                    <div id="wmnh_c" class="accordion-collapse collapse">
                        <p>...</p>
                    </div>
                </div>
            </div>

JS JS

var elementIdToScroll =  window.location.hash;

if(window.location.hash != ''){
  $("#accordion .in").removeClass("in");
  $(elementIdToScroll).addClass("in");
   $('html,body').animate({scrollTop: $(elementIdToScroll).offset().top},'slow');
}

Thanks in advance.提前致谢。 Any help would be appreciated.任何帮助,将不胜感激。

Tested and working in Bootstrap 3.3.5.在 Bootstrap 3.3.5 中测试和工作。

<script type="text/javascript">
$(document).ready(function () {
    if(location.hash != null && location.hash != ""){
        $('.collapse').removeClass('in');
        $(location.hash + '.collapse').collapse('show');
    }
});
</script>

I encountered the same problem just few minutes ago.几分钟前我遇到了同样的问题。 The solution appears to be straight forward - you need to parse an URL and add class in to the matchable accordion, using its id :该解决方案似乎是直截了当-你需要解析的网址,并添加类in的可匹配手风琴,使用ID:

// Opening accordion based on URL
var url = document.location.toString();
if ( url.match('#') ) {
    $('#'+url.split('#')[1]).addClass('in');
}

Tested and working in Bootstrap 3.1.1.在 Bootstrap 3.1.1 中测试和工作。

使用Bootstrap 4.4 ,只需将这一行添加到我的 JS 代码中(在文档就绪子句中)似乎就可以解决问题:

if(location.hash != null && location.hash != ""){$(location.hash + '.collapse').collapse('show');}

I'm using this in Yii2 with the Collapse widget.我在 Yii2 中将它与 Collapse 小部件一起使用。 Assign an id to your panels.为您的面板分配一个 id。
If you have plain html you can just add an id to your a-tag and update the selector.如果您有纯 html,您只需在 a-tag 中添加一个 id 并更新选择器。

$(function(){
    var hash = document.location.hash;
    if (hash) {
        $(hash).find('a').click();
    }
});

Just a response, to Ilia R's.只是对 Ilia R 的回应。 His solution worked great!他的解决方案效果很好! The only thing, though, was that the panel title style wasn't updating (the .collapsed class needed to be removed from the panel title link), so I tweaked Ilia R's code a tad.不过,唯一的问题是面板标题样式没有更新(需要从面板标题链接中删除 .collapsed 类),所以我稍微调整了 Ilia R 的代码。 Someone probably has a better / cleaner solution, but here's a start.有人可能有更好/更清洁的解决方案,但这是一个开始。

        $(document).ready(function() {
            var url = document.location.toString();
            if ( url.match('#') ) {
                $('#'+url.split('#')[1]).addClass('in');
                var cPanelBody = $('#'+url.split('#')[1]);
                var cPanelHeading = cPanelBody.prev();
                cPanelHeading.find( ".panel-title a" ).removeClass('collapsed');
            }
        });

For me worked:对我来说工作:

    $(document).ready(function() {
        var url = document.location.toString();
        if ( url.match('#') ) {
            var cPanelBody = $('#'+url.split('#')[1]);
            cPanelBody.find(".panel-title a")[0].click();
        }
    });

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

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