Javascript 如何在javascript中打开模态而不是手动单击?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30396803/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 05:08:26  来源:igfitidea点击:

How to open a modal in javascript instead of manual click?

javascriptjqueryhtmlangularjstwitter-bootstrap

提问by Pracede

I have a modal which is open when i click on a button. Now i want to open the modal in a javascript function. Here is the working modal :

我有一个模式,当我点击一个按钮时它会打开。现在我想在 javascript 函数中打开模态。这是工作模式:

<a href="#" class="btn btn-lg btn-primary" id="sentMessage" data-toggle="modal" data-target="#largeModal">Click to open Modal</a>
    <div class="modal fade" id="largeModal" tabindex="-1" role="dialog" aria-labelledby="largeModal" aria-hidden="true">
      <div class="modal-dialog modal-lg">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
            <h4 class="modal-title" id="myModalLabel">Large Modal</h4>
          </div>
          <div class="modal-body">
            <h3>Modal Body</h3>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
            <button type="button" class="btn btn-primary">Save changes</button>
          </div>
        </div>
      </div>
    </div>

I want to open the same modal in a javascript function. So to simulate this function, i create another button which trigger a function :

我想在 javascript 函数中打开相同的模式。所以为了模拟这个功能,我创建了另一个触发功能的按钮:

<a href="#" ng-click="openModal();" class="btn btn-lg btn-primary">Click to execute js</a>

Here is my openModal function :

这是我的 openModal 函数:

$scope.openModal = function(){
    $("#sentMessage").click();

};

When i click on the button "Click to execute js", the modal does not open. Here is the plunker

当我单击“单击以执行 js”按钮时,模态不会打开。这里是plunker

回答by Mivaweb

you can use the modal function:

您可以使用模态函数:

$('#largeModal').modal('show');