单击链接,通过 JavaScript 打开 Bootstrap 4 Modal
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/50210529/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Open Bootstrap 4 Modal via JavaScript by clicking a link
提问by Cray
I want to open a modal when I click on a link.
Unfortunately I couldn't change the HTML of the link. I could only change the content of href.
我想在单击链接时打开一个模态。不幸的是,我无法更改链接的 HTML。我只能更改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. I added this code to my site:
在文档中,我找到了一种通过 JavaScript 打开模态的方法。我将此代码添加到我的网站:
$('#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">×</span></button>
            </div>
            <div class="modal-body">
                Content
            </div>
        </div>
    </div>
</div>
回答by Kiran Shahi
Trigger click event on 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">×</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
回答by Cray
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. http://api.jquery.com/category/selectors/
jQuery 提供了多种选择元素的方法。http://api.jquery.com/category/selectors/
$('a[href$="#Modal"]').on( "click", function() {
   $('#Modal').modal();
});`
回答by Ryuk Lee
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">×</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');
});

