[英]How to get the Bootstrap modal without Id?
I have multiple bootstrap modal in my application and I want to call an same function on each modal shown.bs.modal
event. 我在我的应用程序的多个自举模式,我想呼吁各模式的功能相同shown.bs.modal
事件。 I want to do this globally at one place but the thing that bother me is that this event needs an id
attribute of a particular modal like below: 我想在一处全局执行此操作,但令我困扰的是此事件需要特定模式的id
属性,如下所示:
$('#myModal').on('shown.bs.modal', function () {
$('#myInput').focus()
})
I want to know that is there any way to do it globally as whenever some modal shows, then one function call? 我想知道有什么办法可以在全局上做到,只要有模式显示,然后调用一个函数?
$('#myModal')
is simply a jQuery selector. $('#myModal')
只是一个jQuery选择器。 Read more about the jQuery selectors. 阅读有关jQuery选择器的更多信息。 We can write anything withing $()
like: 我们可以使用$()
编写任何内容,例如:
$("#myModal") // Select an element with id "myModal"
$("body > #myModal") // Select an element with id "myModal" which is a immediate child of the "body" tag
$("#foo .modal") // Select all elements with a class "modal" which is inside an element with id "foo"
So, in Bootstrap, every modal has a class .modal
, you can simply use that class to attach a global listener: 因此,在Bootstrap中,每个模式都有一个.modal
类,您可以简单地使用该类来附加一个全局侦听器:
$('.modal').on('shown.bs.modal', function () {
$('#myInput').focus()
});
Or, like others have mentioned, you can give a class to all your modal where you want to do some common stuff like do-common-stuff
: 或者,就像其他人提到的那样,您可以为所有模态提供一个类,以在其中进行一些常见的事情,例如do-common-stuff
:
<div class="modal do-common-stuff" id="foo">...</div>
<div class="modal" id="bar">...</div>
<div class="modal do-common-stuff" id="tango">...</div>
and later, 然后,
$('.do-common-stuff').on('shown.bs.modal', function () {
$('#myInput').focus()
});
You can go with the Class name of model 您可以使用模型的类名称
$('.modelClassName').on('shown.bs.modal', function () {
$('#myInput').focus()
})
Try it with class 上课尝试
$('.classNameOfModal').on('shown.bs.modal', function () {
$('#myInput').focus()
})
Consider accessing through class name. 考虑通过类名进行访问。 Dot '.' 点'。' is used for class. 用于课堂。
$('.myModal').on('shown.bs.modal', function () {
$('#myInput').focus()
})
This is working great for me. 这对我来说很棒。
$(window).on("shown.bs.modal", function () {
//Do what ever you want here...
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.