2014-03-04 29 views
0

我需要能夠得到來自手風琴項目href值,我只是點擊我假定這將(其使用Twitter的崩潰插上)得到一個值超出show.bs.collapse Twitter的引導的

工作鏈接:

<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); 
}) 

,但它不工作,只是記錄 「未定義」

我能夠從中得到一個理想值,其返回undefined是因爲你想提取不存在,因爲this#accordian DIV的href屬性值將href

+1

這是因爲'this'指'#DIV accordian'這 –

回答

6

原因。

您可以通過獲取鏈接而不類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

+0

是我是後 – odd