admin 管理员组文章数量: 1086019
I'm trying to do something (in this case some statistics) whenever a Bootrap modal is opened.
I know, I can ad an event listener like this to a modal:
$('#modal-content').on('shown.bs.modal', function() {
alert('do something');
});
But is there a way to have a listener for all modals on the page? Bonus question: even for those who are inserted by Javascript?
I have thought about something like this:
jQuery('.modal[role="dialog"]').on('show.bs.modal', function() {
alert('do something');
});
But I hope that there's a better way and something that will work for modals that are inserted by Javascript.
I'm trying to do something (in this case some statistics) whenever a Bootrap modal is opened.
I know, I can ad an event listener like this to a modal:
$('#modal-content').on('shown.bs.modal', function() {
alert('do something');
});
But is there a way to have a listener for all modals on the page? Bonus question: even for those who are inserted by Javascript?
I have thought about something like this:
jQuery('.modal[role="dialog"]').on('show.bs.modal', function() {
alert('do something');
});
But I hope that there's a better way and something that will work for modals that are inserted by Javascript.
Share Improve this question asked Feb 19, 2015 at 16:15 wawawawa 5,0845 gold badges35 silver badges59 bronze badges2 Answers
Reset to default 12This ought to do the trick for you--it catches the event when it bubbles up to the document level, so that should catch them all:
$(document).on('shown.bs.modal', function() {
alert('do something');
});
See this bootply
HTH, -Ted
If you know the ids ahead of time, you could just list them out:
$('#modalOne, #modalTwo').on('shown.bs.modal', function() {
alert('do something');
});
Or better yet, if you are creating them programmatically as well as in html and are using a mon prefix for the id, you can use the following:
$("div[id^='modal']").on('shown.bs.modal', function() {
alert('do something');
});
本文标签: javascriptShow event for all Bootstrap Modals on the pageStack Overflow
版权声明:本文标题:javascript - Show event for all Bootstrap Modals on the page - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/p/1744060948a2526707.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论