如何在Bootstrap崩溃事件中获取clicked元素? [英] How to get the clicked element in Bootstrap collapse event?
问题描述
我无法使用Bootstrap 3.3.4在崩溃事件中找到被单击的锚元素:
I am having trouble to find the clicked anchor element in the collapse event using Bootstrap 3.3.4.:
$('.collapse').on('show.bs.collapse', function (e) {
// Get clicked element that initiated the collapse...
});
我需要这个,因为如果单击的元素具有特定的data属性,我想防止崩溃.另一个选择是使用.on('click', function())
进行挂接,但是单击时还会发生其他需要运行的事件,因此对我来说这似乎是行不通的.在DOM中搜索最接近的锚点或类似的标记将无法正常工作,因为彼此之间有多个锚点.
I need this because I'd like to prevent collapsing if the clicked element has a certain data attribute. Another option would be to hook in by using .on('click', function())
, however there are other events that occur on click that need to run so this seems a no-go to me. Searching in the DOM for the closest anchor or something similar won't work as well since there are multiple anchors next to eachother.
推荐答案
由于我的案例中有多个紧挨着的锚点(即不考虑常规的Bootstrap结构),我最终使用折叠的div ID搜索了被点击的锚点:
Since my case featured multiple anchors next to eachother (viz. disregarding regular Bootstrap structure), I ended up searching for the clicked anchor by using the collapsing div's ID:
$('.collapse').on('show.bs.collapse', function (e) {
// Get clicked element that initiated the collapse...
clicked = $(document).find("[href='#" + $(e.target).attr('id') + "']")
});
这篇关于如何在Bootstrap崩溃事件中获取clicked元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!