gpt4 book ai didi

javascript - 获取点击打开 Bootstrap Modal 对话框的元素

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

我如何知道哪个按钮触发了 Bootstrap 模态对话框的打开?

<a href="#" data-toggle="modal" data-target="#myModal">
Button 1
</a>
<a href="#" data-toggle="modal" data-target="#myModal">
Button 2
</a>

Javascript

$('#myModal').on('shown.bs.modal', function () {
var triggerElement = ???
})

最佳答案

其记录 here

$('#myModal').on('shown.bs.modal', function (event) {
var triggerElement = $(event.relatedTarget); // Button that triggered the modal
});

编辑:如评论中所述,如果按钮是动态生成的,我们可以将事件监听器附加到文档:

$(document).on('shown.bs.modal', '#myModal', function (event) {
var triggerElement = $(event.relatedTarget); // Button that triggered
});

关于javascript - 获取点击打开 Bootstrap Modal 对话框的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39647983/

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