简体   繁体   English

如何在没有ID的情况下获得Bootstrap模态?

[英]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.

相关问题 如何在Wordpress的Bootstrap Modal中获取帖子ID - How to get Post ID in bootstrap Modal in Wordpress 如何获取data-id的值并将其置于twitter bootstrap模式中? - How to get value of data-id and place it into a twitter bootstrap modal? 如何使用laravel在每个id的bootstrap模式中获取动态数据? - How to get dynamic data in bootstrap modal for each id by using laravel? 如何通过调用产品 id 来获取模式中的详细信息来制作模式引导弹出窗口? - How to make a modal bootstrap popup by calling the product id to get details in the modal? 在hide.bs.modal上获取引导程序模态元素ID - get bootstrap modal element id on hide.bs.modal Bootstrap 模态只是在不出现的情况下褪色 - Bootstrap modal just get faded without appearing 如何获得bootstrap模态大小 - How to get bootstrap modal size 如何让 Bootstrap 5 Modal Fade Out 效果起作用(除了 Bootstrap,没有 Jquery 和其他附加组件)? - How to get Bootstrap 5 Modal Fade Out effect to work (without Jquery and other add-ons besides Bootstrap)? 如何使用JQuery或JavaScript打开没有ID或类的引导方式对话框? - how to open bootstrap modal-dialog without id or class using JQuery or JavaScript? 如何使用Codeigniter将ID传递给Bootstrap模态? - How to pass id to Bootstrap modal using Codeigniter?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM