从show.bs.collapse twitter bootstrap中获取一个值

odd*_*odd 0 jquery accordion twitter-bootstrap twitter-bootstrap-3

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

链接:

<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne" class="accorLink">
Run Code Online (Sandbox Code Playgroud)

抓住它:

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

但它没有工作,只是记录"未定义"

我能从理想的href中获得一个值

Car*_*all 6

它返回的原因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);
});
Run Code Online (Sandbox Code Playgroud)

JSFiddle演示