我需要能够从我刚刚点击的手风琴项目中获取 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
引用了#accordian
div。
您可以通过获取没有类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