从 show.bs.collapse Twitter bootstrap 中获取值



我需要能够从我刚刚点击的手风琴项目中获取 href 值,我认为这会起作用(它使用 twitters 折叠插件)

链接:

<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" class="accorLink">

抓住它:

$('#accordion').on('show.bs.collapse', function () {
    console.log('foo');
    var sectionID = $(this).attr("href");
    console.log(sectionID);
})

但它不起作用,只是记录"未定义"

理想情况下,我是否能够从中获得值 href

其返回undefined的原因是因为您尝试提取不存在的href属性值,因为this引用了#accordiandiv。

您可以通过获取没有类collapsed的链接来执行此操作:

$('#accordion').on('show.bs.collapse', function () {
    //get the anchor of the accordian that does not has the class "collapsed"
    var openAnchor = $(this).find('a[data-toggle=collapse]:not(.collapsed)');
    //extract the href
    var sectionID = openAnchor.attr('href');
    console.log(sectionID);
});

JSFiddle Demo

最新更新