简体   繁体   English

单击链接,通过JavaScript打开Bootstrap 4 Modal

[英]Open Bootstrap 4 Modal via JavaScript by clicking a link

I want to open a modal when I click on a link. 我想点击一个链接时打开一个模态。 Unfortunately I couldn't change the HTML of the link. 不幸的是我无法更改链接的HTML。 I could only change the content of href . 我只能改变href的内容。

Like this: 像这样:

<a href="#Modal">Open modal</a>

I could not add data-attributes or any other HTML. 我无法添加数据属性或任何其他HTML。

In the docs I found a way to open the modal via JavaScript. 在文档中,我找到了一种通过JavaScript打开模态的方法。 I added this code to my site: 我将此代码添加到我的网站:

$('#Modal').modal();

It works but the modal opens immediately after the page loads. 它可以工作,但模式在页面加载后立即打开。 I need it to open after I click on the link. 点击链接后我需要打开它。

Is there a way to do this? 有没有办法做到这一点?

Here's my modal: 这是我的模态:

<div class="modal" id="Modal" tabindex="-1" role="dialog" aria-labelledby="ModalTitle" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <p class="h5 modal-title" id="ModalTitle">Modal Title</p>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            </div>
            <div class="modal-body">
                Content
            </div>
        </div>
    </div>
</div>

Trigger click event on a[href$="#Modal"] . 触发a[href$="#Modal"]a[href$="#Modal"]点击事件。

 $('a[href$="#Modal"]').on( "click", function() { $('#Modal').modal('show'); }); 
 <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet"/> <!-- Trigger the modal with a button --> <a href="#Modal" class="btn btn-info btn-lg">Open modal</a> <!-- Modal --> <div id="Modal" class="modal fade" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">Modal Header</h4> </div> <div class="modal-body"> <p>Some text in the modal.</p> </div> <div class="modal-footer"> <button type="button" class="btn btn-default" data-dismiss="modal">Close</button> </div> </div> </div> </div> 

EIDT: changed myModal to Modal EIDT:将myModal更改为Modal

need it to open after I click on the link. 点击链接后需要打开它。

So you'll need to check if the link is clicked. 因此,您需要检查是否已单击该链接。

jQuery provides many ways to select an element. jQuery提供了许多选择元素的方法。 http://api.jquery.com/category/selectors/ http://api.jquery.com/category/selectors/

$('a[href$="#Modal"]').on( "click", function() {
   $('#Modal').modal();
});`

Try this 尝试这个

<!-- Trigger the modal with a button -->
<a href="#" class="btn btn-info btn-lg">Open Modal</a>

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">

<!-- Modal content-->
<div class="modal-content">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal">&times;</button>
    <h4 class="modal-title">Modal Header</h4>
  </div>
  <div class="modal-body">
    <p>Some text in the modal.</p>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
  </div>
</div>

  </div>
</div>


$('a').on( "click", function() {
   $('#myModal').modal('show');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM