gpt4 book ai didi

javascript - Bootstrap 3 - 如果 Accordion 位于通过 ajax 填充的模态内,则不会触发事件 hidden.bs.collapse

转载 作者:搜寻专家 更新时间:2023-11-01 05:30:02 25 4
gpt4 key购买 nike

我有一个通过 ajax 填充的简单 Bootstrap 模式。内容是bootstrap插件Collapse做的一个Accordion。如果我尝试检测 hidden.bs.collapse(或任何其他 Event detectable),它不会被触发。

http://jsfiddle.net/Diac/n2jm5cy8/

HTML

<!-- Button trigger modal -->
<a href="/echo/html/" data-remote="false" data-toggle="modal" data-target="#myModal">Launch demo modal</a>
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<h4 class="modal-title" id="myModalLabel">Modal title</h4>
</div>
<div class="modal-body" id="modalbody">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div>
</div>
</div>

accordion.html(通过ajax加载)

<div class='panel-group' id='accordion'>
<div class='panel panel-default' id='panel1'>
<div class='panel-heading'>
<h4 class='panel-title'>
<a data-toggle='collapse' data-parent='#accordion' href='#collapseOne'>Collapsible Group Item #1</a>
</h4>
</div>
<div id='collapseOne' class='panel-collapse collapse in'>
<div class='panel-body'> Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS. </div>
</div>
</div>
</div>

JavaScript

$('a[data-toggle]').click(function(e) {
e.preventDefault();
});

$('#myModal').on('show.bs.modal', function(event) {
$.ajax({
url: "accordion.html",
success: function(responseText) {
$("#modalbody").html(responseText);
}
});
});

$('.panel').on('hidden.bs.collapse', function (e) {
alert('Event fired on #' + e.currentTarget.id);
})

最佳答案

您正在尝试在 DOM 中存在任何具有 .panel 类的元素之前,将您的事件监听器附加到具有 .panel 类的元素。

为了让您的代码工作,您可以在 AJAX 成功回调中移动您的 $('.panel').on(...),在 .panels 实际插入到DOM。或者,可能最好,您可以改用事件委托(delegate):

$('#modalbody').on('hidden.bs.collapse', '.panel', function (e) {
alert('Event fired on #' + e.currentTarget.id);
});

请参阅 jQuery documentation 中有关事件委托(delegate)的讨论。 .

相关摘录:

Event handlers are bound only to the currently selected elements; they must exist at the time your code makes the call to .on(). To ensure the elements are present and can be selected, place scripts after the elements in the HTML markup or perform event binding inside a document ready handler. Alternatively, use delegated events to attach event handlers.

关于javascript - Bootstrap 3 - 如果 Accordion 位于通过 ajax 填充的模态内,则不会触发事件 hidden.bs.collapse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33131618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com